[ovs-dev] [kernel-reload 8/8] xenserver: Introduce "force-reload-kmod" to reload kernel module.

Ben Pfaff blp at nicira.com
Wed Feb 2 22:31:22 UTC 2011


Running "service openvswitch force-reload-kmod" will now save the kernel
configuration state of Open vSwitch interfaces, stop the vswitch, unload
the kernel module, reload the kernel module, restart the vswitch, and
restore kernel configuration state.  It is a reasonably safe way to upgrade
or downgrade the Open vSwitch kernel module on a running system.

Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 utilities/automake.mk            |    1 +
 utilities/ovs-save               |  134 ++++++++++++++++++++++++++++++++++++++
 xenserver/etc_init.d_openvswitch |   44 ++++++++++++
 xenserver/openvswitch-xen.spec   |    5 +-
 4 files changed, 183 insertions(+), 1 deletions(-)
 create mode 100755 utilities/ovs-save

diff --git a/utilities/automake.mk b/utilities/automake.mk
index 7aa5800..36b2b91 100644
--- a/utilities/automake.mk
+++ b/utilities/automake.mk
@@ -31,6 +31,7 @@ EXTRA_DIST += \
 	utilities/ovs-pki-cgi.in \
 	utilities/ovs-pki.8.in \
 	utilities/ovs-pki.in \
+	utilities/ovs-save \
 	utilities/ovs-tcpundump.1.in \
 	utilities/ovs-tcpundump.in \
 	utilities/ovs-vlan-test.in \
diff --git a/utilities/ovs-save b/utilities/ovs-save
new file mode 100755
index 0000000..76f02da
--- /dev/null
+++ b/utilities/ovs-save
@@ -0,0 +1,134 @@
+#! /bin/sh
+
+# Copyright (c) 2011 Nicira Networks, 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.
+
+if test "X$1" = X--help; then
+    cat <<EOF
+$0: saves the kernel configuration of network interfaces
+usage: $0 NETDEV...
+
+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.
+
+This script is meant as a helper for the Open vSwitch init
+script "force-reload-kmod" command.
+EOF
+    exit 0
+fi
+
+PATH=/sbin:/bin:/usr/sbin:/usr/bin
+
+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` || 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 down # Required to change hwaddr.
+        echo ip link set $dev $linkcmd
+    fi
+
+    # IP addresses (including IPv6).
+    echo "ip addr flush $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)
+                    # 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.
+                        continue 2
+                    fi
+                    ;;
+                "$dev")
+                    # Omit because "ip" wants "dev" keyword in front.
+                    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
+
+        echo "ip route add $route dev $dev"
+    done
+
+    echo
+done
+
+echo "# global"
+echo "iptables-restore <<'EOF'"
+iptables-save
+echo "EOF"
diff --git a/xenserver/etc_init.d_openvswitch b/xenserver/etc_init.d_openvswitch
index a99cc9f..85cb97b 100755
--- a/xenserver/etc_init.d_openvswitch
+++ b/xenserver/etc_init.d_openvswitch
@@ -445,6 +445,47 @@ function restart {
     fi
 }
 
+function 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.
+    #
+    #   - 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
+}
+
+function force_reload_kmod {
+    ifaces=$(internal_interfaces)
+    action "Configured internal interfaces: $ifaces" true
+
+    stop
+
+    script=$(mktemp)
+    action "Save interface configuration to $script" true
+    /usr/share/openvswitch/scripts/ovs-save $ifaces > $script
+    chmod +x $script
+
+    action "Destroy datapaths" remove_all_dp
+
+    remove_modules
+
+    start
+
+    action "Restore interface configuration from $script" $script
+}
+
 case "$1" in
     start)
         start
@@ -480,6 +521,9 @@ case "$1" in
         /usr/sbin/ovs-vswitchd -V
         /usr/sbin/ovs-brcompatd -V
         ;;
+    force-reload-kmod)
+	force_reload_kmod
+        ;;
     help)
         printf "openvswitch [start|stop|restart|reload|force-reload|status|version]\n"
         ;;
diff --git a/xenserver/openvswitch-xen.spec b/xenserver/openvswitch-xen.spec
index deb1fd3..9a05090 100644
--- a/xenserver/openvswitch-xen.spec
+++ b/xenserver/openvswitch-xen.spec
@@ -1,6 +1,6 @@
 # Spec file for Open vSwitch.
 
-# Copyright (C) 2009, 2010 Nicira Networks, Inc.
+# Copyright (C) 2009, 2010, 2011 Nicira Networks, Inc.
 #
 # Copying and distribution of this file, with or without modification,
 # are permitted in any medium without royalty provided the copyright
@@ -83,6 +83,8 @@ install -m 755 xenserver/usr_sbin_brctl \
              $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/brctl
 install -m 755 xenserver/usr_share_openvswitch_scripts_sysconfig.template \
          $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/sysconfig.template
+install -m 755 utilities/ovs-save \
+         $RPM_BUILD_ROOT/usr/share/openvswitch/scripts/ovs-save
 install -d -m 755 $RPM_BUILD_ROOT/usr/lib/xsconsole/plugins-base
 install -m 644 \
         xenserver/usr_lib_xsconsole_plugins-base_XSFeatureVSwitch.py \
@@ -402,6 +404,7 @@ fi
 /usr/share/openvswitch/scripts/xen-bugtool
 /usr/share/openvswitch/scripts/brctl
 /usr/share/openvswitch/scripts/sysconfig.template
+/usr/share/openvswitch/scripts/ovs-save
 /usr/share/openvswitch/vswitch.ovsschema
 /usr/sbin/ovs-brcompatd
 /usr/sbin/ovs-vswitchd
-- 
1.7.1





More information about the dev mailing list