netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / utilities / ovs-lib.in
1 # This is a shell function library sourced by some Open vSwitch scripts.
2 # It is not intended to be invoked on its own.
3
4 # Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at:
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 ## ----------------- ##
19 ## configure options ##
20 ## ----------------- ##
21
22 # All of these should be substituted by the Makefile at build time.
23 logdir=${OVS_LOGDIR-'@LOGDIR@'}                 # /var/log/openvswitch
24 rundir=${OVS_RUNDIR-'@RUNDIR@'}                 # /var/run/openvswitch
25 sysconfdir=${OVS_SYSCONFDIR-'@sysconfdir@'}     # /etc
26 etcdir=$sysconfdir/openvswitch                  # /etc/openvswitch
27 datadir=${OVS_PKGDATADIR-'@pkgdatadir@'}        # /usr/share/openvswitch
28 bindir=${OVS_BINDIR-'@bindir@'}                 # /usr/bin
29 sbindir=${OVS_SBINDIR-'@sbindir@'}              # /usr/sbin
30
31 # /etc/openvswitch or /var/lib/openvswitch
32 if test X"$OVS_DBDIR" != X; then
33     dbdir=$OVS_DBDIR
34 elif test X"$OVS_SYSCONFDIR" != X; then
35     dbdir=$OVS_SYSCONFDIR/openvswitch
36 else
37     dbdir='@DBDIR@'
38 fi
39
40 ovs_ctl_log () {
41     echo "$@" >> "${logdir}/ovs-ctl.log"
42 }
43
44 ovs_ctl () {
45     case "$@" in
46         *"=strace"*)
47             # In case of running the daemon with strace, piping the o/p causes
48             # the script to block (strace probably does not close the inherited
49             # pipe). So, do not log the o/p to ovs-ctl.log.
50             "${datadir}/scripts/ovs-ctl" "$@"
51         ;;
52         "status")
53             # In case of the command 'status', we should return the exit status
54             # of ovs-ctl. It is also useful to document the o/p in ovs-ctl.log.
55             display=`"${datadir}/scripts/ovs-ctl" "$@" 2>&1`
56             rc=$?
57             if test -w "${logdir}/ovs-ctl.log"; then
58                  echo "${display}" | tee -a "${logdir}/ovs-ctl.log"
59             else
60                  echo "${display}"
61             fi
62             return ${rc}
63         ;;
64         *)
65             echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
66             "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
67         ;;
68     esac
69 }
70
71 VERSION='@VERSION@'
72
73 DAEMON_CWD=/
74
75 LC_ALL=C; export LC_ALL
76
77 ## ------------- ##
78 ## LSB functions ##
79 ## ------------- ##
80
81 # Use the system's own implementations if it has any.
82 if test -e /etc/init.d/functions; then
83     . /etc/init.d/functions
84 elif test -e /etc/rc.d/init.d/functions; then
85     . /etc/rc.d/init.d/functions
86 elif test -e /lib/lsb/init-functions; then
87     . /lib/lsb/init-functions
88 fi
89
90 # Implement missing functions (e.g. OpenSUSE lacks 'action').
91 if type log_success_msg >/dev/null 2>&1; then :; else
92     log_success_msg () {
93         printf '%s.\n' "$*"
94     }
95 fi
96 if type log_failure_msg >/dev/null 2>&1; then :; else
97     log_failure_msg () {
98         printf '%s ... failed!\n' "$*"
99     }
100 fi
101 if type log_warning_msg >/dev/null 2>&1; then :; else
102     log_warning_msg () {
103         printf '%s ... (warning).\n' "$*"
104     }
105 fi
106 if type action >/dev/null 2>&1; then :; else
107     action () {
108        STRING=$1
109        shift
110        "$@"
111        rc=$?
112        if test $rc = 0; then
113             log_success_msg "$STRING"
114        else
115             log_failure_msg "$STRING"
116        fi
117        return $rc
118     }
119 fi
120
121 ## ------- ##
122 ## Daemons ##
123 ## ------- ##
124
125 pid_exists () {
126     # This is better than "kill -0" because it doesn't require permission to
127     # send a signal (so daemon_status in particular works as non-root).
128     test -d /proc/"$1"
129 }
130
131 pid_comm_check () {
132     [ "$1" = "`cat /proc/$2/comm`" ]
133 }
134
135 start_daemon () {
136     priority=$1
137     wrapper=$2
138     shift; shift
139     daemon=$1
140     strace=""
141
142     # drop core files in a sensible place
143     test -d "$DAEMON_CWD" || install -d -m 755 -o root -g root "$DAEMON_CWD"
144     set "$@" --no-chdir
145     cd "$DAEMON_CWD"
146
147     # log file
148     test -d "$logdir" || install -d -m 755 -o root -g root "$logdir"
149     set "$@" --log-file="$logdir/$daemon.log"
150
151     # pidfile and monitoring
152     test -d "$rundir" || install -d -m 755 -o root -g root "$rundir"
153     set "$@" --pidfile="$rundir/$daemon.pid"
154     set "$@" --detach --monitor
155
156     # wrapper
157     case $wrapper in
158         valgrind)
159             if (valgrind --version) > /dev/null 2>&1; then
160                 set valgrind -q --leak-check=full --time-stamp=yes \
161                     --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
162             else
163                 log_failure_msg "valgrind not installed, running $daemon without it"
164             fi
165             ;;
166         strace)
167             if (strace -V) > /dev/null 2>&1; then
168                 strace="strace -tt -T -s 256 -ff"
169                 if (strace -DV) > /dev/null 2>&1; then
170                     # Has the -D option.
171                     set $strace -D -o "$logdir/$daemon.strace.log" "$@"
172                     strace=""
173                 fi
174             else
175                 log_failure_msg "strace not installed, running $daemon without it"
176             fi
177             ;;
178         glibc)
179             set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
180             ;;
181         '')
182             ;;
183         *)
184             log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
185             ;;
186     esac
187
188     # priority
189     if test X"$priority" != X; then
190         set nice -n "$priority" "$@"
191     fi
192
193     action "Starting $daemon" "$@"
194
195     if test X"$strace" != X; then
196         # Strace doesn't have the -D option so we attach after the fact.
197         setsid $strace -o "$logdir/$daemon.strace.log" \
198             -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
199     fi
200 }
201
202 stop_daemon () {
203     if test -e "$rundir/$1.pid"; then
204         if pid=`cat "$rundir/$1.pid"`; then
205             for action in EXIT .1 .25 .65 1 \
206                           TERM .1 .25 .65 1 1 1 1 \
207                           KILL 1 1 1 2 10 15 30 \
208                           FAIL; do
209                 if pid_exists "$pid" >/dev/null 2>&1; then :; else
210                     return 0
211                 fi
212                 case $action in
213                     EXIT)
214                         action "Exiting $1 ($pid)" \
215                             ${bindir}/ovs-appctl -T 1 -t $rundir/$1.$pid.ctl exit
216                         ;;
217                     TERM)
218                         action "Killing $1 ($pid)" kill $pid
219                         ;;
220                     KILL)
221                         action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
222                         ;;
223                     FAIL)
224                         log_failure_msg "Killing $1 ($pid) failed"
225                         return 1
226                         ;;
227                     *)
228                         sleep $action
229                         ;;
230                 esac
231             done
232         fi
233     fi
234     log_success_msg "$1 is not running"
235 }
236
237 daemon_status () {
238     pidfile=$rundir/$1.pid
239     if test -e "$pidfile"; then
240         if pid=`cat "$pidfile"`; then
241             if pid_exists "$pid"; then
242                 echo "$1 is running with pid $pid"
243                 return 0
244             else
245                 echo "Pidfile for $1 ($pidfile) is stale"
246             fi
247         else
248             echo "Pidfile for $1 ($pidfile) exists but cannot be read"
249         fi
250     else
251         echo "$1 is not running"
252     fi
253     return 1
254 }
255
256 daemon_is_running () {
257     pidfile=$rundir/$1.pid
258     test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid" && pid_comm_check $1 $pid
259 } >/dev/null 2>&1
260
261 # Prints commands needed to move the ip address from interface $1 to interface
262 # $2
263 move_ip_address () {
264     if [ -z "$1" ] || [ -z "$2" ]; then
265         return
266     fi
267     dev="$1"
268     dst="$2"
269
270     # IP addresses (including IPv6).
271     echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
272     ip addr show dev $dev | while read addr; do
273         set -- $addr
274
275         # Check and trim family.
276         family=$1
277         shift
278         case $family in
279             inet | inet6) ;;
280             *) continue ;;
281         esac
282
283         # Trim device off the end--"ip" insists on having "dev" precede it.
284         addrcmd=
285         while test $# != 0; do
286             case $1 in
287                 dynamic)
288                     # Omit kernel-maintained route.
289                     continue 2
290                     ;;
291                 scope)
292                     if test "$2" = link -a "$family" != inet6; then
293                         # Omit route derived from IP address, e.g.
294                         # 172.16.0.0/16 derived from 172.16.12.34,
295                         # but preserve IPv6 link-local address.
296                         continue 2
297                     fi
298                     ;;
299                 "$dev"|"$dev:"*)
300                     # Address label string
301                     label=`echo $1 | sed "s/$dev/$dst/"`
302                     addrcmd="$addrcmd label $label"
303                     shift
304                     continue
305                     ;;
306             esac
307             addrcmd="$addrcmd $1"
308             shift
309         done
310         if test "$1" != "$dev"; then
311             addrcmd="$addrcmd $1"
312         fi
313
314         echo ip -f $family addr add $addrcmd dev $dst
315     done
316 }
317
318 # Prints commands needed to move the ip route of interface $1 to interface $2
319 move_ip_routes () {
320     if [ -z "$1" ] || [ -z "$2" ]; then
321         return
322     fi
323     dev="$1"
324     dst="$2"
325     echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
326     ip route show dev $dev | while read route; do
327         # "proto kernel" routes are installed by the kernel automatically.
328         case $route in
329             *" proto kernel "*) continue ;;
330         esac
331
332         echo "ip route add $route dev $dst"
333     done
334 }
335
336 ovsdb_tool () {
337     ovsdb-tool -vconsole:off "$@"
338 }
339
340 create_db () {
341     DB_FILE="$1"
342     DB_SCHEMA="$2"
343     action "Creating empty database $DB_FILE" ovsdb_tool create "$DB_FILE" "$DB_SCHEMA"
344 }
345
346 upgrade_db () {
347     DB_FILE="$1"
348     DB_SCHEMA="$2"
349
350     schemaver=`ovsdb_tool schema-version "$DB_SCHEMA"`
351     if test ! -e "$DB_FILE"; then
352         log_warning_msg "$DB_FILE does not exist"
353         install -d -m 755 -o root -g root `dirname $DB_FILE`
354         create_db "$DB_FILE" "$DB_SCHEMA"
355     elif test X"`ovsdb_tool needs-conversion "$DB_FILE" "$DB_SCHEMA"`" != Xno; then
356         # Back up the old version.
357         version=`ovsdb_tool db-version "$DB_FILE"`
358         cksum=`ovsdb_tool db-cksum "$DB_FILE" | awk '{print $1}'`
359         backup=$DB_FILE.backup$version-$cksum
360         action "Backing up database to $backup" cp "$DB_FILE" "$backup" || return 1
361
362         # Compact database.  This is important if the old schema did not enable
363         # garbage collection (i.e. if it did not have any tables with "isRoot":
364         # true) but the new schema does.  In that situation the old database
365         # may contain a transaction that creates a record followed by a
366         # transaction that creates the first use of the record.  Replaying that
367         # series of transactions against the new database schema (as "convert"
368         # does) would cause the record to be dropped by the first transaction,
369         # then the second transaction would cause a referential integrity
370         # failure (for a strong reference).
371         #
372         # Errors might occur on an Open vSwitch downgrade if ovsdb-tool doesn't
373         # understand some feature of the schema used in the OVSDB version that
374         # we're downgrading from, so we don't give up on error.
375         action "Compacting database" ovsdb_tool compact "$DB_FILE"
376
377         # Upgrade or downgrade schema.
378         if action "Converting database schema" ovsdb_tool convert "$DB_FILE" "$DB_SCHEMA"; then
379             :
380         else
381             log_warning_msg "Schema conversion failed, using empty database instead"
382             rm -f "$DB_FILE"
383             create_db "$DB_FILE" "$DB_SCHEMA"
384         fi
385     fi
386 }