This file is indexed.

/usr/share/openvswitch/scripts/ovs-lib is in openvswitch-common 2.9.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# This is a shell function library sourced by some Open vSwitch scripts.
# It is not intended to be invoked on its own.

# Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

## ----------------- ##
## configure options ##
## ----------------- ##

# All of these should be substituted by the Makefile at build time.
logdir=${OVS_LOGDIR-'/var/log/openvswitch'}                 # /var/log/openvswitch
rundir=${OVS_RUNDIR-'/var/run/openvswitch'}                 # /var/run/openvswitch
sysconfdir=${OVS_SYSCONFDIR-'/etc'}     # /etc
etcdir=$sysconfdir/openvswitch                  # /etc/openvswitch
datadir=${OVS_PKGDATADIR-'/usr/share/openvswitch'}        # /usr/share/openvswitch
bindir=${OVS_BINDIR-'/usr/bin'}                 # /usr/bin
sbindir=${OVS_SBINDIR-'/usr/sbin'}              # /usr/sbin

# /etc/openvswitch or /var/lib/openvswitch
if test X"$OVS_DBDIR" != X; then
    dbdir=$OVS_DBDIR
elif test X"$OVS_SYSCONFDIR" != X; then
    dbdir=$OVS_SYSCONFDIR/openvswitch
else
    dbdir='/etc/openvswitch'
fi

ovs_ctl_log () {
    echo "$@" >> "${logdir}/ovs-ctl.log"
}

ovs_ctl () {
    case "$@" in
        *"=strace"*)
            # In case of running the daemon with strace, piping the o/p causes
            # the script to block (strace probably does not close the inherited
            # pipe). So, do not log the o/p to ovs-ctl.log.
            "${datadir}/scripts/ovs-ctl" "$@"
        ;;
        "status")
            # In case of the command 'status', we should return the exit status
            # of ovs-ctl. It is also useful to document the o/p in ovs-ctl.log.
            display=`"${datadir}/scripts/ovs-ctl" "$@" 2>&1`
            rc=$?
            if test -w "${logdir}/ovs-ctl.log"; then
                 echo "${display}" | tee -a "${logdir}/ovs-ctl.log"
            else
                 echo "${display}"
            fi
            return ${rc}
        ;;
        *)
            echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
            "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
        ;;
    esac
}

VERSION='2.9.0'

DAEMON_CWD=/

LC_ALL=C; export LC_ALL

## ------------- ##
## LSB functions ##
## ------------- ##

# Use the system's own implementations if it has any.
if test -e /etc/init.d/functions; then
    . /etc/init.d/functions
elif test -e /etc/rc.d/init.d/functions; then
    . /etc/rc.d/init.d/functions
elif test -e /lib/lsb/init-functions; then
    . /lib/lsb/init-functions
fi

# Implement missing functions (e.g. OpenSUSE lacks 'action').
if type log_success_msg >/dev/null 2>&1; then :; else
    log_success_msg () {
        printf '%s.\n' "$*"
    }
fi
if type log_failure_msg >/dev/null 2>&1; then :; else
    log_failure_msg () {
        printf '%s ... failed!\n' "$*"
    }
fi
if type log_warning_msg >/dev/null 2>&1; then :; else
    log_warning_msg () {
        printf '%s ... (warning).\n' "$*"
    }
fi
if type action >/dev/null 2>&1; then :; else
    action () {
       STRING=$1
       shift
       "$@"
       rc=$?
       if test $rc = 0; then
            log_success_msg "$STRING"
       else
            log_failure_msg "$STRING"
       fi
       return $rc
    }
fi

## ------- ##
## Daemons ##
## ------- ##

pid_exists () {
    # This is better than "kill -0" because it doesn't require permission to
    # send a signal (so daemon_status in particular works as non-root).
    test -d /proc/"$1"
}

pid_comm_check () {
    [ "$1" = "`cat /proc/$2/comm`" ]
}

# version_geq version_a version_b
#
# Compare (dot separated) version numbers.  Returns true (exit code 0) if
# version_a is greater or equal than version_b, otherwise false (exit code 1).
version_geq() {
    echo $1 $2 | awk '{
        n1 = split($1, a, ".");
        n2 = split($2, b, ".");
        n = (n1 > n2) ? n1 : n2;
        for (i = 1; i <= n; i++) {
            if (a[i]+0 < b[i]+0) exit 1
            if (a[i]+0 > b[i]+0) exit 0
        }
    }'
}

install_dir () {
    DIR="$1"
    INSTALL_MODE="${2:-755}"
    INSTALL_USER="root"
    INSTALL_GROUP="root"
    [ "$OVS_USER" != "" ] && INSTALL_USER="${OVS_USER%:*}"
    [ "${OVS_USER##*:}" != "" ] && INSTALL_GROUP="${OVS_USER##*:}"

    if test ! -d "$DIR"; then
        install -d -m "$INSTALL_MODE" -o "$INSTALL_USER" -g "$INSTALL_GROUP" "$DIR"
        restorecon "$DIR" >/dev/null 2>&1
    fi
}

start_daemon () {
    priority=$1
    wrapper=$2
    shift; shift
    daemon=$1
    strace=""

    # drop core files in a sensible place
    install_dir "$DAEMON_CWD"
    set "$@" --no-chdir
    cd "$DAEMON_CWD"

    # log file
    install_dir "$logdir" "750"
    set "$@" --log-file="$logdir/$daemon.log"

    # pidfile and monitoring
    install_dir "$rundir"
    set "$@" --pidfile="$rundir/$daemon.pid"
    set "$@" --detach
    test X"$MONITOR" = Xno || set "$@" --monitor

    # wrapper
    case $wrapper in
        valgrind)
            if (valgrind --version) > /dev/null 2>&1; then
                set valgrind -q --leak-check=full --time-stamp=yes \
                    --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
            else
                log_failure_msg "valgrind not installed, running $daemon without it"
            fi
            ;;
        strace)
            if (strace -V) > /dev/null 2>&1; then
                strace="strace -tt -T -s 256 -ff"
                if (strace -DV) > /dev/null 2>&1; then
                    # Has the -D option.
                    set $strace -D -o "$logdir/$daemon.strace.log" "$@"
                    strace=""
                fi
            else
                log_failure_msg "strace not installed, running $daemon without it"
            fi
            ;;
        glibc)
            set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
            ;;
        '')
            ;;
        *)
            log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
            ;;
    esac

    # priority
    if test X"$priority" != X; then
        set nice -n "$priority" "$@"
    fi

    action "Starting $daemon" "$@" || return 1

    if test X"$strace" != X; then
        # Strace doesn't have the -D option so we attach after the fact.
        setsid $strace -o "$logdir/$daemon.strace.log" \
            -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
    fi
}

stop_daemon () {
    if test -e "$rundir/$1.pid"; then
        if pid=`cat "$rundir/$1.pid"`; then

            graceful="EXIT .1 .25 .65 1"
            actions="TERM .1 .25 .65 1 1 1 1 \
                     KILL 1 1 1 2 10 15 30 \
                     FAIL"
            version=`ovs-appctl -T 1 -t $rundir/$1.$pid.ctl version \
                     | awk 'NR==1{print $NF}'`

            # Use `ovs-appctl exit` only if the running daemon version
            # is >= 2.5.90.  This script might be used during upgrade to
            # stop older versions of daemons which do not behave correctly
            # with `ovs-appctl exit` (e.g. ovs-vswitchd <= 2.5.0 deletes
            # internal ports).
            if version_geq "$version" "2.5.90"; then
                actions="$graceful $actions"
            fi
            for action in $actions; do
                if pid_exists "$pid" >/dev/null 2>&1; then :; else
                    return 0
                fi
                case $action in
                    EXIT)
                        action "Exiting $1 ($pid)" \
                            ${bindir}/ovs-appctl -T 1 -t $rundir/$1.$pid.ctl exit
                        ;;
                    TERM)
                        action "Killing $1 ($pid)" kill $pid
                        ;;
                    KILL)
                        action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
                        ;;
                    FAIL)
                        log_failure_msg "Killing $1 ($pid) failed"
                        return 1
                        ;;
                    *)
                        sleep $action
                        ;;
                esac
            done
        fi
    fi
    log_success_msg "$1 is not running"
}

daemon_status () {
    pidfile=$rundir/$1.pid
    if test -e "$pidfile"; then
        if pid=`cat "$pidfile"`; then
            if pid_exists "$pid"; then
                echo "$1 is running with pid $pid"
                return 0
            else
                echo "Pidfile for $1 ($pidfile) is stale"
            fi
        else
            echo "Pidfile for $1 ($pidfile) exists but cannot be read"
        fi
    else
        echo "$1 is not running"
    fi
    return 1
}

daemon_is_running () {
    pidfile=$rundir/$1.pid
    test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid" && pid_comm_check $1 $pid
} >/dev/null 2>&1

# Prints commands needed to move the ip address from interface $1 to interface
# $2
move_ip_address () {
    if [ -z "$1" ] || [ -z "$2" ]; then
        return
    fi
    dev="$1"
    dst="$2"

    # IP addresses (including IPv6).
    echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
    ip addr show dev $dev | while read addr; do
        set -- $addr

        # Check and trim family.
        family=$1
        shift
        case $family in
            inet | inet6) ;;
            *) continue ;;
        esac

        # Trim device off the end--"ip" insists on having "dev" precede it.
        addrcmd=
        while test $# != 0; do
            case $1 in
                dynamic)
                    # XXX: According to 'man ip-address', "dynamic" is only
                    # used for ipv6 addresses.  But, atleast on RHEL 7.4
                    # (iproute-3.10.0-87), it is being used for ipv4
                    # addresses assigned with dhcp.
                    if [ "$family" = "inet" ]; then
                        shift
                        continue
                    fi
                    # Omit kernel-maintained route.
                    continue 2
                    ;;
                scope)
                    if test "$2" = link -a "$family" != inet6; then
                        # Omit route derived from IP address, e.g.
                        # 172.16.0.0/16 derived from 172.16.12.34,
                        # but preserve IPv6 link-local address.
                        continue 2
                    fi
                    ;;
                "$dev"|"$dev:"*)
                    # Address label string
                    label=`echo $1 | sed "s/$dev/$dst/"`
                    addrcmd="$addrcmd label $label"
                    shift
                    continue
                    ;;
            esac
            addrcmd="$addrcmd $1"
            shift
        done
        if test "$1" != "$dev"; then
            addrcmd="$addrcmd $1"
        fi

        echo ip -f $family addr add $addrcmd dev $dst
    done
}

# Prints commands needed to move the ip route of interface $1 to interface $2
move_ip_routes () {
    if [ -z "$1" ] || [ -z "$2" ]; then
        return
    fi
    dev="$1"
    dst="$2"
    echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
    ip route show dev $dev | while read route; do
        # "proto kernel" routes are installed by the kernel automatically.
        case $route in
            *" proto kernel "*) continue ;;
        esac

        echo "ip route add $route dev $dst"
    done
}

ovsdb_tool () {
    if [ "$OVS_USER" != "" ]; then
        runuser --user "${OVS_USER%:*}" -- ovsdb-tool -vconsole:off "$@"
    else
        ovsdb-tool -vconsole:off "$@"
    fi
}

create_db () {
    DB_FILE="$1"
    DB_SCHEMA="$2"
    action "Creating empty database $DB_FILE" ovsdb_tool create "$DB_FILE" "$DB_SCHEMA"
}

upgrade_db () {
    DB_FILE="$1"
    DB_SCHEMA="$2"

    schemaver=`ovsdb_tool schema-version "$DB_SCHEMA"`
    if test ! -e "$DB_FILE"; then
        log_warning_msg "$DB_FILE does not exist"
        install_dir `dirname $DB_FILE`
        create_db "$DB_FILE" "$DB_SCHEMA"
    elif test X"`ovsdb_tool needs-conversion "$DB_FILE" "$DB_SCHEMA"`" = Xyes; then
        # Back up the old version.
        version=`ovsdb_tool db-version "$DB_FILE"`
        cksum=`ovsdb_tool db-cksum "$DB_FILE" | awk '{print $1}'`
        backup=$DB_FILE.backup$version-$cksum
        action "Backing up database to $backup" cp "$DB_FILE" "$backup" || return 1

        # Compact database.  This is important if the old schema did not enable
        # garbage collection (i.e. if it did not have any tables with "isRoot":
        # true) but the new schema does.  In that situation the old database
        # may contain a transaction that creates a record followed by a
        # transaction that creates the first use of the record.  Replaying that
        # series of transactions against the new database schema (as "convert"
        # does) would cause the record to be dropped by the first transaction,
        # then the second transaction would cause a referential integrity
        # failure (for a strong reference).
        #
        # Errors might occur on an Open vSwitch downgrade if ovsdb-tool doesn't
        # understand some feature of the schema used in the OVSDB version that
        # we're downgrading from, so we don't give up on error.
        action "Compacting database" ovsdb_tool compact "$DB_FILE"

        # Upgrade or downgrade schema.
        if action "Converting database schema" ovsdb_tool convert "$DB_FILE" "$DB_SCHEMA"; then
            :
        else
            log_warning_msg "Schema conversion failed, using empty database instead"
            rm -f "$DB_FILE"
            create_db "$DB_FILE" "$DB_SCHEMA"
        fi
    fi
}

ovs_vsctl () {
    ovs-vsctl --no-wait "$@"
}

## ----------------- ##
## force-reload-kmod ##
## ----------------- ##

internal_interfaces () {
    # Outputs a list of internal interfaces:
    #
    #   - There is an internal interface for every bridge, whether it
    #     has an Interface record or not and whether the Interface
    #     record's 'type' is properly set or not.
    #
    #   - There is an internal interface for each Interface record whose
    #     'type' is 'internal'.
    #
    # But ignore interfaces that don't really exist.
    for d in `(ovs_vsctl --bare \
                -- --columns=name find Interface type=internal \
                    -- list-br) | sort -u`
    do
        if test -e "/sys/class/net/$d"; then
                printf "%s " "$d"
            fi
    done
}

ovs_save () {
    bridges=`ovs_vsctl -- --real list-br`
    if [ -n "${bridges}" ] && \
        "$datadir/scripts/ovs-save" "$1" ${bridges} > "$2"; then
        chmod +x "$2"
        return 0
    fi
    [ -z "${bridges}" ] && return 0
}

save_flows_if_required () {
    if test X"$DELETE_BRIDGES" != Xyes; then
        action "Saving flows" ovs_save save-flows "${script_flows}"
    fi
}

save_interfaces () {
    "$datadir/scripts/ovs-save" save-interfaces ${ifaces} \
        > "${script_interfaces}"
}

flow_restore_wait () {
    if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
        ovs_vsctl set open_vswitch . other_config:flow-restore-wait="true"
    fi
}

flow_restore_complete () {
    if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
        ovs_vsctl --if-exists remove open_vswitch . other_config \
                  flow-restore-wait="true"
    fi
}

restore_flows () {
    [ -x "${script_flows}" ] && \
        action "Restoring saved flows" "${script_flows}"
}

restore_interfaces () {
    [ ! -x "${script_interfaces}" ] && return 0
    action "Restoring interface configuration" "${script_interfaces}"
    rc=$?
    if test $rc = 0; then
        level=debug
    else
        level=err
    fi
    log="logger -p daemon.$level -t ovs-save"
    $log "interface restore script exited with status $rc:"
    $log -f "$script_interfaces"
}

init_restore_scripts () {
    script_interfaces=`mktemp`
    script_flows=`mktemp`
    trap 'rm -f "${script_interfaces}" "${script_flows}"' 0
}

force_reload_kmod () {

    if test X"${OVS_VSWITCHD:-yes}" != Xyes; then
        log_failure_msg "Reloading of kmod without ovs-vswitchd is an error"
        exit 1
    fi

    ifaces=`internal_interfaces`
    action "Detected internal interfaces: $ifaces" true

    init_restore_scripts
    save_flows_if_required

    # Restart the database first, since a large database may take a
    # while to load, and we want to minimize forwarding disruption.
    stop_ovsdb
    start_ovsdb || return 1

    stop_forwarding

    if action "Saving interface configuration" save_interfaces; then
        :
    else
        log_warning_msg "Failed to save configuration, not replacing kernel module"
        start_forwarding
        add_managers
        exit 1
    fi
    chmod +x "$script_interfaces"

    for dp in `ovs-dpctl dump-dps`; do
        action "Removing datapath: $dp" ovs-dpctl del-dp "$dp"
    done

    for vport in `awk '/^vport_/ { print $1 }' /proc/modules`; do
        action "Removing $vport module" rmmod $vport
    done

    if test -e /sys/module/openvswitch; then
        action "Removing openvswitch module" rmmod openvswitch
    fi

    # Start vswitchd by asking it to wait till flow restore is finished.
    flow_restore_wait
    start_forwarding || return 1

    # Restore saved flows and inform vswitchd that we are done.
    restore_flows
    flow_restore_complete
    add_managers

    restore_interfaces

    "$datadir/scripts/ovs-check-dead-ifs"
}

## ------- ##
## restart ##
## ------- ##

restart () {
    if daemon_is_running ovsdb-server && daemon_is_running ovs-vswitchd; then
        init_restore_scripts
        if test X"${OVS_VSWITCHD:-yes}" = Xyes; then
            save_flows_if_required
        fi
    fi

    # Restart the database first, since a large database may take a
    # while to load, and we want to minimize forwarding disruption.
    stop_ovsdb
    start_ovsdb || return 1

    stop_forwarding

    # Start vswitchd by asking it to wait till flow restore is finished.
    flow_restore_wait
    start_forwarding || return 1

    # Restore saved flows and inform vswitchd that we are done.
    restore_flows
    flow_restore_complete
    add_managers
}