]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/ovs-save
ovs-vsctl: Add datapath_type column to show command.
[mirror_ovs.git] / utilities / ovs-save
CommitLineData
f7a122fc
BP
1#! /bin/sh
2
c416eaf8 3# Copyright (c) 2011, 2013, 2016 Nicira, Inc.
f7a122fc
BP
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
7a62e5cc
GS
17case $0 in
18 */*) dir0=`echo "$0" | sed 's,/[^/]*$,,'` ;;
19 *) dir0=./ ;;
20esac
21. "$dir0/ovs-lib" || exit 1
22
a4175433
GS
23usage() {
24 UTIL=$(basename $0)
f7a122fc 25 cat <<EOF
a4175433
GS
26${UTIL}: Provides helper functions to save Open vSwitch's configuration.
27usage: $0 COMMAND
f7a122fc 28
a4175433
GS
29Commands:
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
ea274df9 35 OpenFlow flows of each Open vSwitch bridge.
a4175433 36This script is meant as a helper for the Open vSwitch init script commands.
f7a122fc 37EOF
a4175433 38}
f7a122fc 39
a4175433 40save_interfaces () {
5c928528 41 if (ip link show) > /dev/null 2>&1; then :; else
a4175433
GS
42 echo "$0: ip not found in $PATH" >&2
43 exit 1
f7a122fc 44 fi
a4175433
GS
45
46 if test "$#" = 0; then
47 exit 0
f7a122fc
BP
48 fi
49
a4175433
GS
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 ;;
f7a122fc 64 esac
a4175433
GS
65 if expr "$state" : '.*\bdynamic\b' > /dev/null; then
66 linkcmd="$linkcmd dynamic"
67 fi
a3176cb5 68 if qlen=`expr "$state" : '.*qlen \([0-9]\+\)'`; then
a4175433
GS
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
a3176cb5 77 if mtu=`expr "$state" : '.*mtu \([0-9]\+\)'`; then
a4175433
GS
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
a1d5e459 85 move_ip_address $dev $dev
7a62e5cc 86
a1d5e459 87 move_ip_routes $dev $dev
f7a122fc 88
a4175433 89 echo
f7a122fc
BP
90 done
91
4e4cdb43 92 if (iptables-save) > /dev/null 2>&1; then
a4175433
GS
93 echo "# global"
94 echo "iptables-restore <<'EOF'"
95 iptables-save
96 echo "EOF"
4e4cdb43
GS
97 else
98 echo "# iptables-save not found in $PATH, not saving iptables state"
a4175433
GS
99 fi
100}
101
d3b41dd9
TR
102get_highest_ofp_version() {
103 ovs-vsctl get bridge "$1" protocols | \
104 awk -F '"' '{ print (NF>1)? $(NF-1) : "OpenFlow14" }'
105}
106
a4175433 107save_flows () {
4e4cdb43 108 if (ovs-ofctl --version) > /dev/null 2>&1; then :; else
a4175433
GS
109 echo "$0: ovs-ofctl not found in $PATH" >&2
110 exit 1
111 fi
f7a122fc 112
463a0578 113 workdir=$(mktemp -d "${TMPDIR:-/tmp}/ovs-save.XXXXXXXXXX")
a4175433 114 for bridge in "$@"; do
d3b41dd9
TR
115 # Get the highest enabled OpenFlow version
116 ofp_version=$(get_highest_ofp_version "$bridge")
117
221df99c 118 printf "%s" "ovs-ofctl add-tlv-map ${bridge} '"
f069903d 119 ovs-ofctl dump-tlv-map ${bridge} | \
b2b3f8ce 120 awk '/^ *0x/ {if (cnt != 0) printf ","; \
67762449
JG
121 cnt++;printf "{class="$1",type="$2",len="$3"}->"$4}'
122 echo "'"
123
83cf865e
ZW
124 # If possible use OpenFlow 1.4 atomic bundle txn for flows and groups
125 [ ${ofp_version#OpenFlow} -ge 14 ] && bundle=" --bundle" || bundle=""
d3b41dd9 126
83cf865e
ZW
127 echo "ovs-ofctl -O $ofp_version add-groups ${bridge} \
128 \"$workdir/$bridge.groups.dump\" ${bundle}"
129
130 echo "ovs-ofctl -O $ofp_version replace-flows ${bridge} \
131 \"$workdir/$bridge.flows.dump\" ${bundle}"
132
133 ovs-ofctl -O $ofp_version dump-groups "$bridge" | \
134 sed -e '/^OFPST_GROUP_DESC/d' \
135 -e '/^NXST_GROUP_DESC/d' > \
136 "$workdir/$bridge.groups.dump"
d3b41dd9
TR
137
138 ovs-ofctl -O $ofp_version dump-flows --no-names --no-stats "$bridge" | \
139 sed -e '/NXST_FLOW/d' \
140 -e '/OFPST_FLOW/d' \
463a0578
TR
141 -e 's/\(idle\|hard\)_age=[^,]*,//g' > \
142 "$workdir/$bridge.flows.dump"
f7a122fc 143 done
463a0578 144 echo "rm -rf \"$workdir\""
a4175433 145}
f7a122fc 146
a4175433
GS
147while [ $# -ne 0 ]
148do
149 case $1 in
a4175433
GS
150 "save-flows")
151 shift
152 save_flows "$@"
153 exit 0
154 ;;
155 "save-interfaces")
156 shift
157 save_interfaces "$@"
158 exit 0
159 ;;
160 -h | --help)
161 usage
162 exit 0
163 ;;
164 *)
165 echo >&2 "$0: unknown command \"$1\" (use --help for help)"
166 exit 1
167 ;;
168 esac
169done
f7a122fc
BP
170
171exit 0