]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-save
dpctl: Fix usage for most dpctl commands.
[mirror_ovs.git] / utilities / ovs-save
1 #! /bin/sh
2
3 # Copyright (c) 2011, 2013, 2016 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 case $0 in
18 */*) dir0=`echo "$0" | sed 's,/[^/]*$,,'` ;;
19 *) dir0=./ ;;
20 esac
21 . "$dir0/ovs-lib" || exit 1
22
23 usage() {
24 UTIL=$(basename $0)
25 cat <<EOF
26 ${UTIL}: Provides helper functions to save Open vSwitch's configuration.
27 usage: $0 COMMAND
28
29 Commands:
30 save-interfaces Outputs a shell script on stdout that will restore
31 the current kernel configuration of the specified
32 network interfaces, as well as the system iptables
33 configuration.
34 save-flows Outputs a shell script on stdout that will restore
35 OpenFlow flows of each Open vSwitch bridge.
36 This script is meant as a helper for the Open vSwitch init script commands.
37 EOF
38 }
39
40 save_interfaces () {
41 if (ip -V) > /dev/null 2>&1; then :; else
42 echo "$0: ip not found in $PATH" >&2
43 exit 1
44 fi
45
46 if test "$#" = 0; then
47 exit 0
48 fi
49
50 devs="$@"
51 for dev in $devs; do
52 state=`ip link show dev $dev` || continue
53
54 echo "# $dev"
55 # Link state (Ethernet addresses, up/down, ...)
56 linkcmd=
57 case $state in
58 *"state UP"* | *[,\<]"UP"[,\>]* )
59 linkcmd="$linkcmd up"
60 ;;
61 *"state DOWN"*)
62 linkcmd="$linkcmd down"
63 ;;
64 esac
65 if expr "$state" : '.*\bdynamic\b' > /dev/null; then
66 linkcmd="$linkcmd dynamic"
67 fi
68 if qlen=`expr "$state" : '.*qlen \([0-9]\+\)'`; then
69 linkcmd="$linkcmd txqueuelen $qlen"
70 fi
71 if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
72 linkcmd="$linkcmd address $hwaddr"
73 fi
74 if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
75 linkcmd="$linkcmd broadcast $brd"
76 fi
77 if mtu=`expr "$state" : '.*mtu \([0-9]\+\)'`; then
78 linkcmd="$linkcmd mtu $mtu"
79 fi
80 if test -n "$linkcmd"; then
81 echo ip link set dev $dev down # Required to change hwaddr.
82 echo ip link set dev $dev $linkcmd
83 fi
84
85 move_ip_address $dev $dev
86
87 move_ip_routes $dev $dev
88
89 echo
90 done
91
92 if (iptables-save) > /dev/null 2>&1; then
93 echo "# global"
94 echo "iptables-restore <<'EOF'"
95 iptables-save
96 echo "EOF"
97 else
98 echo "# iptables-save not found in $PATH, not saving iptables state"
99 fi
100 }
101
102 save_flows () {
103 if (ovs-ofctl --version) > /dev/null 2>&1; then :; else
104 echo "$0: ovs-ofctl not found in $PATH" >&2
105 exit 1
106 fi
107
108 for bridge in "$@"; do
109 echo -n "ovs-ofctl add-tlv-map ${bridge} '"
110 ovs-ofctl dump-tlv-map ${bridge} | \
111 awk '/^ 0x/ {if (cnt != 0) printf ","; \
112 cnt++;printf "{class="$1",type="$2",len="$3"}->"$4}'
113 echo "'"
114
115 echo "ovs-ofctl add-flows ${bridge} - << EOF"
116 ovs-ofctl dump-flows "${bridge}" | sed -e '/NXST_FLOW/d' \
117 -e 's/\(idle\|hard\)_age=[^,]*,//g'
118 echo "EOF"
119 done
120 }
121
122 while [ $# -ne 0 ]
123 do
124 case $1 in
125 "save-flows")
126 shift
127 save_flows "$@"
128 exit 0
129 ;;
130 "save-interfaces")
131 shift
132 save_interfaces "$@"
133 exit 0
134 ;;
135 -h | --help)
136 usage
137 exit 0
138 ;;
139 *)
140 echo >&2 "$0: unknown command \"$1\" (use --help for help)"
141 exit 1
142 ;;
143 esac
144 done
145
146 exit 0