netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / utilities / ovs-save
1 #! /bin/sh
2
3 # Copyright (c) 2011, 2013 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  save-ofports           Outputs a shell script on stdout that will restore
37                         the ofport value across a force-reload-kmod.
38 This script is meant as a helper for the Open vSwitch init script commands.
39 EOF
40 }
41
42 save_interfaces () {
43     if (ip -V) > /dev/null 2>&1; then :; else
44         echo "$0: ip not found in $PATH" >&2
45         exit 1
46     fi
47
48     if test "$#" = 0; then
49         exit 0
50     fi
51
52     devs="$@"
53     for dev in $devs; do
54         state=`ip link show dev $dev` || continue
55
56         echo "# $dev"
57         # Link state (Ethernet addresses, up/down, ...)
58         linkcmd=
59         case $state in
60             *"state UP"* | *[,\<]"UP"[,\>]* )
61                 linkcmd="$linkcmd up"
62                 ;;
63             *"state DOWN"*)
64                 linkcmd="$linkcmd down"
65                 ;;
66         esac
67         if expr "$state" : '.*\bdynamic\b' > /dev/null; then
68             linkcmd="$linkcmd dynamic"
69         fi
70         if qlen=`expr "$state" : '.*qlen \([0-9]+\)'`; then
71             linkcmd="$linkcmd txqueuelen $qlen"
72         fi
73         if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
74             linkcmd="$linkcmd address $hwaddr"
75         fi
76         if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
77             linkcmd="$linkcmd broadcast $brd"
78         fi
79         if mtu=`expr "$state" : '.*mtu \([0-9]+\)'`; then
80             linkcmd="$linkcmd mtu $mtu"
81         fi
82         if test -n "$linkcmd"; then
83             echo ip link set dev $dev down # Required to change hwaddr.
84             echo ip link set dev $dev $linkcmd
85         fi
86
87         move_ip_address $dev $dev
88
89         move_ip_routes $dev $dev
90
91         echo
92     done
93
94     if (iptables-save) > /dev/null 2>&1; then
95         echo "# global"
96         echo "iptables-restore <<'EOF'"
97         iptables-save
98         echo "EOF"
99     else
100         echo "# iptables-save not found in $PATH, not saving iptables state"
101     fi
102 }
103
104 save_flows () {
105     if (ovs-ofctl --version) > /dev/null 2>&1; then :; else
106         echo "$0: ovs-ofctl not found in $PATH" >&2
107         exit 1
108     fi
109
110     for bridge in "$@"; do
111         echo "ovs-ofctl add-flows ${bridge} - << EOF"
112         ovs-ofctl dump-flows "${bridge}" | sed -e '/NXST_FLOW/d' \
113             -e 's/\(idle\|hard\)_age=[^,]*,//g'
114         echo "EOF"
115     done
116 }
117
118 ovs_vsctl () {
119     ovs-vsctl --no-wait "$@"
120 }
121
122 save_ofports ()
123 {
124     if (ovs-vsctl --version) > /dev/null 2>&1; then :; else
125         echo "$0: ovs-vsctl not found in $PATH" >&2
126         exit 1
127     fi
128
129     for bridge in "$@"; do
130         count=0
131         for iface in `ovs_vsctl list-ifaces ${bridge}`; do
132             ofport=`ovs_vsctl get interface ${iface} ofport`
133             [ "${count}" -eq 0 ] && cmd="ovs-vsctl --no-wait"
134             cmd="${cmd} -- --if-exists set interface "${iface}" \
135                      ofport_request="${ofport}""
136
137             # Run set interface command on 50 ports at a time.
138             count=`expr ${count} + 1`
139             [ "${count}" -eq 50 ] && count=0 && echo "${cmd}" && cmd=""
140         done
141         echo "${cmd}"
142     done
143 }
144
145 while [ $# -ne 0 ]
146 do
147     case $1 in
148         "save-flows")
149             shift
150             save_flows "$@"
151             exit 0
152             ;;
153         "save-interfaces")
154             shift
155             save_interfaces "$@"
156             exit 0
157             ;;
158         "save-ofports")
159             shift
160             save_ofports "$@"
161             exit 0
162             ;;
163         -h | --help)
164             usage
165             exit 0
166             ;;
167         *)
168             echo >&2 "$0: unknown command \"$1\" (use --help for help)"
169             exit 1
170             ;;
171     esac
172 done
173
174 exit 0