Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / utilities / ovs-save
1 #! /bin/sh
2
3 # Copyright (c) 2011 Nicira Networks, 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 if test "X$1" = X--help; then
18     cat <<EOF
19 $0: saves the kernel configuration of network interfaces
20 usage: $0 NETDEV...
21
22 Outputs a shell script on stdout that will restore the current
23 kernel configuration of the specified network interfaces, as
24 well as the system iptables configuration.
25
26 This script is meant as a helper for the Open vSwitch init
27 script "force-reload-kmod" command.
28 EOF
29     exit 0
30 fi
31
32 PATH=/sbin:/bin:/usr/sbin:/usr/bin
33
34 missing_program () {
35     save_IFS=$IFS
36     IFS=:
37     for dir in $PATH; do
38         IFS=$save_IFS
39         if test -x $dir/$1; then
40             return 1
41         fi
42     done
43     IFS=$save_IFS
44     return 0
45 }
46 if missing_program ip; then 
47     echo "$0: ip not found in $PATH" >&2
48     exit 1
49 fi
50
51 if test "$#" = 0; then
52     echo "# $0: no parameters given (use \"$0 --help\" for help)"
53 fi
54
55 devs=$*
56 for dev in $devs; do
57     state=`ip link show $dev` || continue
58
59     echo "# $dev"
60     # Link state (Ethernet addresses, up/down, ...)
61     linkcmd=
62     case $state in
63         *"state UP"* | *[,\<]"UP"[,\>]* )
64             linkcmd="$linkcmd up"
65             ;;
66         *"state DOWN"*)
67             linkcmd="$linkcmd down"
68             ;;
69     esac
70     if expr "$state" : '.*\bdynamic\b' > /dev/null; then
71         linkcmd="$linkcmd dynamic"
72     fi
73     if qlen=`expr "$state" : '.*qlen \([0-9]+\)'`; then
74         linkcmd="$linkcmd txqueuelen $qlen"
75     fi
76     if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
77         linkcmd="$linkcmd address $hwaddr"
78     fi
79     if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
80         linkcmd="$linkcmd broadcast $brd"
81     fi
82     if mtu=`expr "$state" : '.*mtu \([0-9]+\)'`; then
83         linkcmd="$linkcmd mtu $mtu"
84     fi
85     if test -n "$linkcmd"; then
86         echo ip link set $dev down # Required to change hwaddr.
87         echo ip link set $dev $linkcmd
88     fi
89
90     # IP addresses (including IPv6).
91     echo "ip addr flush $dev 2>/dev/null" # Suppresses "Nothing to flush".
92     ip addr show dev $dev | while read addr; do
93         set -- $addr
94
95         # Check and trim family.
96         family=$1
97         shift
98         case $family in
99             inet | inet6) ;;
100             *) continue ;;
101         esac
102
103         # Trim device off the end--"ip" insists on having "dev" precede it.
104         addrcmd=
105         while test $# != 0; do
106             case $1 in
107                 dynamic)
108                     # Omit kernel-maintained route.
109                     continue 2
110                     ;;
111                 scope)
112                     if test "$2" = link; then
113                         # Omit route derived from IP address, e.g.
114                         # 172.16.0.0/16 derived from 172.16.12.34.
115                         continue 2
116                     fi
117                     ;;
118                 "$dev")
119                     # Omit because "ip" wants "dev" keyword in front.
120                     shift
121                     continue
122                     ;;
123             esac
124             addrcmd="$addrcmd $1"
125             shift
126         done
127         if test "$1" != "$dev"; then
128             addrcmd="$addrcmd $1"
129         fi
130
131         echo ip -f $family addr add $addrcmd dev $dev
132     done
133
134     # Routes.
135     echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
136     ip route show dev $dev | while read route; do
137         # "proto kernel" routes are installed by the kernel automatically.
138         case $route in
139             *" proto kernel "*) continue ;;
140         esac
141
142         echo "ip route add $route dev $dev"
143     done
144
145     echo
146 done
147
148 if missing_program iptables-save; then
149     echo "# iptables-save not found in $PATH, not saving iptables state"
150 else
151     echo "# global"
152     echo "iptables-restore <<'EOF'"
153     iptables-save
154     echo "EOF"
155 fi
156
157 exit 0