summaryrefslogtreecommitdiff
path: root/utilities/ovs-save
blob: 67092ecf7ee703023467c0e7c02cd260bc6bd267 (plain)
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
#! /bin/sh

# Copyright (c) 2011, 2013, 2016 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.

case $0 in
    */*) dir0=`echo "$0" | sed 's,/[^/]*$,,'` ;;
    *) dir0=./ ;;
esac
. "$dir0/ovs-lib" || exit 1

usage() {
    UTIL=$(basename $0)
    cat <<EOF
${UTIL}: Provides helper functions to save Open vSwitch's configuration.
usage: $0 COMMAND

Commands:
 save-interfaces        Outputs a shell script on stdout that will restore
                        the current kernel configuration of the specified
                        network interfaces, as well as the system iptables
                        configuration.
 save-flows             Outputs a shell script on stdout that will restore
                        OpenFlow flows of each Open vSwitch bridge.
This script is meant as a helper for the Open vSwitch init script commands.
EOF
}

save_interfaces () {
    if (ip link show) > /dev/null 2>&1; then :; else
        echo "$0: ip not found in $PATH" >&2
        exit 1
    fi

    if test "$#" = 0; then
        exit 0
    fi

    devs="$@"
    for dev in $devs; do
        state=`ip link show dev $dev` || continue

        echo "# $dev"
        # Link state (Ethernet addresses, up/down, ...)
        linkcmd=
        case $state in
            *"state UP"* | *[,\<]"UP"[,\>]* )
                linkcmd="$linkcmd up"
                ;;
            *"state DOWN"*)
                linkcmd="$linkcmd down"
                ;;
        esac
        if expr "$state" : '.*\bdynamic\b' > /dev/null; then
            linkcmd="$linkcmd dynamic"
        fi
        if qlen=`expr "$state" : '.*qlen \([0-9]\+\)'`; then
            linkcmd="$linkcmd txqueuelen $qlen"
        fi
        if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
            linkcmd="$linkcmd address $hwaddr"
        fi
        if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
            linkcmd="$linkcmd broadcast $brd"
        fi
        if mtu=`expr "$state" : '.*mtu \([0-9]\+\)'`; then
            linkcmd="$linkcmd mtu $mtu"
        fi
        if test -n "$linkcmd"; then
            echo ip link set dev $dev down # Required to change hwaddr.
            echo ip link set dev $dev $linkcmd
        fi

        move_ip_address $dev $dev

        move_ip_routes $dev $dev

        echo
    done

    if (iptables-save) > /dev/null 2>&1; then
        echo "# global"
        echo "iptables-restore <<'EOF'"
        iptables-save
        echo "EOF"
    else
        echo "# iptables-save not found in $PATH, not saving iptables state"
    fi
}

get_highest_ofp_version() {
    ovs-vsctl get bridge "$1" protocols | \
        sed 's/[][]//g' | sed 's/\ //g' | \
            awk -F ',' '{ print (NF>0)? $(NF) : "OpenFlow14" }'
}

save_flows () {
    if (ovs-ofctl --version) > /dev/null 2>&1; then :; else
        echo "$0: ovs-ofctl not found in $PATH" >&2
        exit 1
    fi

    # OVS 2.7 and earlier do not enable OpenFlow 1.4 (by default) and lack
    # other features needed to save and restore flows.  Don't try.
    case `ovs-appctl version | sed 1q` in
        "ovs-vswitchd (Open vSwitch) 1."*.*)
            return
            ;;
        "ovs-vswitchd (Open vSwitch) 2."[0-7].*)
            return
            ;;
    esac

    workdir=$(mktemp -d "${TMPDIR:-/tmp}/ovs-save.XXXXXXXXXX")
    for bridge in "$@"; do
        # Get the highest enabled OpenFlow version
        ofp_version=$(get_highest_ofp_version "$bridge")

        printf "%s" "ovs-ofctl -O $ofp_version add-tlv-map ${bridge} '"
        ovs-ofctl dump-tlv-map ${bridge} -O $ofp_version | \
        awk '/^  *0x/ {if (cnt != 0) printf ","; \
             cnt++;printf "{class="$1",type="$2",len="$3"}->"$4}'
        echo "'"

        # If possible use OpenFlow 1.4 atomic bundle txn for flows and groups
        [ ${ofp_version#OpenFlow} -ge 14 ] && bundle=" --bundle" || bundle=""

        echo "ovs-ofctl -O $ofp_version add-groups ${bridge} \
              \"$workdir/$bridge.groups.dump\" ${bundle}"

        echo "ovs-ofctl -O $ofp_version replace-flows ${bridge} \
              \"$workdir/$bridge.flows.dump\" ${bundle}"

        ovs-ofctl -O $ofp_version dump-groups "$bridge" | \
            sed -e '/^OFPST_GROUP_DESC/d' \
                -e '/^NXST_GROUP_DESC/d' > \
                "$workdir/$bridge.groups.dump"

        ovs-ofctl -O $ofp_version dump-flows --no-names --no-stats "$bridge" | \
            sed -e '/NXST_FLOW/d' \
                -e '/OFPST_FLOW/d' \
                -e 's/\(idle\|hard\)_age=[^,]*,//g' \
                -e 's/igmp_type/tp_src/g' \
                -e 's/igmp_code/tp_dst/g' \
                -e 's/igmp/ip,nw_proto=2/g' > \
                "$workdir/$bridge.flows.dump"
    done
    echo "rm -rf \"$workdir\""
}

while [ $# -ne 0 ]
do
    case $1 in
        "save-flows")
            shift
            save_flows "$@"
            exit 0
            ;;
        "save-interfaces")
            shift
            save_interfaces "$@"
            exit 0
            ;;
        -h | --help)
            usage
            exit 0
            ;;
        *)
            echo >&2 "$0: unknown command \"$1\" (use --help for help)"
            exit 1
            ;;
    esac
done

exit 0