X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=utilities%2Fovs-save;h=73895f3375ec71f1418588a35b69a19206d7336a;hb=HEAD;hp=de3129042d27592acda272e83de5d8b256158b7a;hpb=200940deb7c85de7afe3e52edabf574088ac01e3;p=sliver-openvswitch.git diff --git a/utilities/ovs-save b/utilities/ovs-save index de3129042..73895f337 100755 --- a/utilities/ovs-save +++ b/utilities/ovs-save @@ -1,6 +1,6 @@ #! /bin/sh -# Copyright (c) 2011 Nicira Networks, Inc. +# Copyright (c) 2011, 2013 Nicira, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -14,145 +14,207 @@ # See the License for the specific language governing permissions and # limitations under the License. -if test "X$1" = X--help; then +usage() { + UTIL=$(basename $0) cat <&2 - exit 1 -fi - -if test "$#" = 0; then - echo "# $0: no parameters given (use \"$0 --help\" for help)" -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" + +save_interfaces () { + if (ip -V) > /dev/null 2>&1; then :; else + echo "$0: ip not found in $PATH" >&2 + exit 1 fi - if test -n "$linkcmd"; then - echo ip link set dev $dev down # Required to change hwaddr. - echo ip link set dev $dev $linkcmd + + if test "$#" = 0; then + exit 0 fi - # 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 ;; + 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 + + # 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 - # Trim device off the end--"ip" insists on having "dev" precede it. - addrcmd= - while test $# != 0; do - case $1 in - dynamic) - # Omit kernel-maintained route. - continue 2 - ;; - scope) - if test "$2" = link; then - # Omit route derived from IP address, e.g. - # 172.16.0.0/16 derived from 172.16.12.34. + # 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) + # Omit kernel-maintained route. continue 2 - fi - ;; - "$dev"|"$dev:"*) - # Address label string - addrcmd="$addrcmd label $1" - shift - continue - ;; + ;; + scope) + if test "$2" = link; then + # Omit route derived from IP address, e.g. + # 172.16.0.0/16 derived from 172.16.12.34. + continue 2 + fi + ;; + "$dev"|"$dev:"*) + # Address label string + addrcmd="$addrcmd label $1" + 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 $dev + done + + # Routes. + 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 - addrcmd="$addrcmd $1" - shift + + echo "ip route add $route dev $dev" done - if test "$1" != "$dev"; then - addrcmd="$addrcmd $1" - fi - echo ip -f $family addr add $addrcmd dev $dev + echo done - # Routes. - 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 + 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 +} + +save_flows () { + if (ovs-ofctl --version) > /dev/null 2>&1; then :; else + echo "$0: ovs-ofctl not found in $PATH" >&2 + exit 1 + fi - echo "ip route add $route dev $dev" + for bridge in "$@"; do + echo "ovs-ofctl add-flows ${bridge} - << EOF" + ovs-ofctl dump-flows "${bridge}" | sed -e '/NXST_FLOW/d' \ + -e 's/\(idle\|hard\)_age=[^,]*,//g' + echo "EOF" done +} - echo -done +ovs_vsctl () { + ovs-vsctl --no-wait "$@" +} + +save_ofports () +{ + if (ovs-vsctl --version) > /dev/null 2>&1; then :; else + echo "$0: ovs-vsctl not found in $PATH" >&2 + exit 1 + fi + + for bridge in "$@"; do + count=0 + for iface in `ovs_vsctl list-ifaces ${bridge}`; do + ofport=`ovs_vsctl get interface ${iface} ofport` + [ "${count}" -eq 0 ] && cmd="ovs-vsctl --no-wait" + cmd="${cmd} -- --if-exists set interface "${iface}" \ + ofport_request="${ofport}"" + + # Run set interface command on 50 ports at a time. + count=`expr ${count} + 1` + [ "${count}" -eq 50 ] && count=0 && echo "${cmd}" && cmd="" + done + echo "${cmd}" + done +} -if missing_program iptables-save; then - echo "# iptables-save not found in $PATH, not saving iptables state" -else - echo "# global" - echo "iptables-restore <<'EOF'" - iptables-save - echo "EOF" -fi +while [ $# -ne 0 ] +do + case $1 in + "save-flows") + shift + save_flows "$@" + exit 0 + ;; + "save-interfaces") + shift + save_interfaces "$@" + exit 0 + ;; + "save-ofports") + shift + save_ofports "$@" + exit 0 + ;; + -h | --help) + usage + exit 0 + ;; + *) + echo >&2 "$0: unknown command \"$1\" (use --help for help)" + exit 1 + ;; + esac +done exit 0