Fix vswitch init.d file
authorKeith Amidon <keith@nicira.com>
Wed, 18 Mar 2009 18:20:07 +0000 (11:20 -0700)
committerKeith Amidon <keith@nicira.com>
Wed, 18 Mar 2009 20:22:33 +0000 (13:22 -0700)
- Add options for restarting under strace and valgrind
- Make updating modules work again
- Optimize datapath add/remove using showdp output

vswitchd/etc/init.d/vswitch

index e6956d5045e1663f5e9f346a5138a707ed6f1c73..e4443ce7dad25ada71c29b80bcb110e5c770bbf5 100755 (executable)
@@ -14,12 +14,29 @@ FILE_LOGLEVEL="${FILE_LOGLEVEL:-}"
 PRIORITY="${PRIORITY:--5}"
 MEMLEAK_LOG="${MEMLEAK_LOG:-}"
 
+# --- Debugging startup options ---
+#
+# These should not generally be used since it requires vswitchd to run
+# in the foreground which is not really what you want in an init
+# script.  Used by the restart-valgrind and restart-strace commands
+
+STRACE_LOG="${STRACE_LOG:-}"
+STRACE_OPT="${STRACE_OPT:-}"
+VALGRIND_LOG="${VALGRIND_LOG:-}"
+VALGRIND_OPT="${VALGRIND_OPT:-}"
+
 VSWITCH_BASE=/root/vswitch/openflow/build
 VSWITCHD_CONF=/etc/vswitchd.conf
 VSWITCHD_LOG=/var/log/vswitchd.log
 
+function dp_list {
+    $VSWITCH_BASE/utilities/dpctl showdp | grep '^dp[0-9]\+:' | cut -d':' -f 1
+}
+
 function dp_intf {
-       $VSWITCH_BASE/utilities/dpctl show nl:$1 | grep '^ LOCAL(' | cut -d'(' -f2 | cut -d')' -f1
+    local dp=$1
+    # Currently port0 is hardcoded to be the local port.
+    $VSWITCH_BASE/utilities/dpctl showdp $dp | grep 'port 0:' | cut -d' ' -f 3
 }
 
 function clear_old_bridge_ports {
@@ -28,12 +45,12 @@ function clear_old_bridge_ports {
 }
 
 function start {
-       if ! lsmod | grep -q "openflow_mod"; then
-               action "Inserting openflow module" insmod $VSWITCH_BASE/datapath/linux-2.6/openflow_mod.ko
-       fi
-       if ! lsmod | grep -q "brcompat_mod"; then
-               action "Inserting brcompat module" insmod $VSWITCH_BASE/datapath/linux-2.6/brcompat_mod.ko
-       fi
+    if ! lsmod | grep -q "openflow_mod"; then
+        action "Inserting openflow module" insmod $VSWITCH_BASE/datapath/linux-2.6/openflow_mod.ko
+    fi
+    if ! lsmod | grep -q "brcompat_mod"; then
+        action "Inserting brcompat module" insmod $VSWITCH_BASE/datapath/linux-2.6/brcompat_mod.ko
+    fi
     ulimit -c unlimited   # Ensure core dump on crash. Will be in '/'.
     local syslog_opt="-vANY:SYSLOG:${SYSLOG_LOGLEVEL}"
     local logfile_file_opt=""
@@ -49,41 +66,48 @@ function start {
             mv "$MEMLEAK_LOG" "$MEMLEAK_LOG.prev"
         fi
     fi
-    [ "$1" = "restart" ] || clear_old_bridge_ports
-       action "Starting vswitchd" nice -n "$PRIORITY" $VSWITCH_BASE/vswitchd/vswitchd -P/var/run/vswitchd.pid -D -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt --brcompat $leak_opt "$VSWITCHD_CONF"
-       if [ "$1" = "restart" ]; then
+    local strace_opt=""
+    local daemonize_opt="-D"
+    if [ -n "$STRACE_LOG" ] && [ -n "$VALGRIND_LOG" ]; then
+        printf "Can not start with both VALGRIND and STRACE\n"
+        exit 1
+    fi
+    if [ -n "$STRACE_LOG" ]; then
+        strace_opt="strace -o $STRACE_LOG $STRACE_OPT"
+        daemonize_opt=""
+    fi
+    if [ -n "$VALGRIND_LOG" ]; then
+        valgrind_opt="valgrind --log-file=$VALGRIND_LOG $VALGRIND_OPT"
+        daemonize_opt=""
+    fi
+    [ "$1" = "update-modules" ] || [ "$1" = "restart" ] || clear_old_bridge_ports
+       action "Starting vswitchd" nice -n "$PRIORITY" $strace_opt $valgrind_opt $VSWITCH_BASE/vswitchd/vswitchd -P/var/run/vswitchd.pid $daemonize_opt -vANY:CONSOLE:EMER $syslog_opt $logfile_level_opt $logfile_file_opt --brcompat $leak_opt "$VSWITCHD_CONF"
+       if [ "$1" = "update-modules" ]; then
                sleep 2    # Give time for vswitch to get up and running.
-               i=0
-               while test $i -lt 256; do
-                       if $VSWITCH_BASE/utilities/dpctl show nl:$i >/dev/null 2>&1; then
-                               intf=$(dp_intf $i)
-                               if [ -e "/etc/sysconfig/network-scripts/ifcfg-$intf" ]; then
-                                       action "Bringing up datapath interface: $intf" ifup "$intf"
-                               fi
+        for dp in $(dp_list); do
+                       local intf=$(dp_intf $dp)
+                       if [ -e "/etc/sysconfig/network-scripts/ifcfg-$intf" ]; then
+                               action "Bringing up datapath interface: $intf" ifup "$intf"
                        fi
-                       i=$((i + 1))
                done
        fi
 }
 
 function stop {
        if [ -f /var/run/vswitchd.pid ]; then
-               action "Killing vswitchd" kill -TERM $(cat /var/run/vswitchd.pid)
+        local pid=$(cat /var/run/vswitchd.pid)
+               action "Killing vswitchd ($pid)" kill -TERM $pid
        fi
        if [ -e /var/run/vswitchd.pid ]; then
                rm -f /var/run/vswitchd.pid
        fi
-       if [ "$1" = "restart" ]; then
-               i=0
-               while test $i -lt 256; do
-                       if $VSWITCH_BASE/utilities/dpctl show nl:$i >/dev/null 2>&1; then
-                               intf=$(dp_intf $i)
-                               if [ -e "/etc/sysconfig/network-scripts/ifcfg-$intf" ]; then
-                                       action "Shutting down datapath interface: $intf" ifdown "$intf"
-                               fi
-                               action "Removing openflow datapath $i" $VSWITCH_BASE/utilities/dpctl deldp nl:$i
+       if [ "$1" = "update-modules" ]; then
+        for dp in $(dp_list); do
+                       local intf=$(dp_intf $dp)
+                       action "Removing datapath: $dp" $VSWITCH_BASE/utilities/dpctl deldp $dp
+                       if [ -e "/etc/sysconfig/network-scripts/ifcfg-$intf" ]; then
+                               action "Shutting down datapath interface: $intf" ifdown "$intf"
                        fi
-                       i=$((i + 1))
                done
                action "Removing brcompat module" rmmod brcompat_mod.ko
                action "Removing openflow module" rmmod openflow_mod.ko
@@ -98,16 +122,34 @@ case "$1" in
                stop
                ;;
        restart)
-               stop restart
+               stop
                start restart
                ;;
+    restart-strace)
+        shift
+        stop
+        STRACE_LOG="/var/log/vswitchd.strace" STRACE_OPT="$*" start restart
+        ;;
+    restart-valgrind)
+        shift
+        stop
+        VALGRIND_LOG="/var/log/vswitchd.valgrind" VALGRIND_OPT="$*" start restart
+        ;;
+    update-modules)
+        stop update-modules
+        start update-modules
+        ;;
        reload)
                if [ -f /var/run/vswitchd.pid ]; then
                        kill -HUP $(cat /var/run/vswitchd.pid)
                fi
                ;;
+    strace)
+        shift
+        strace -p $(cat /var/run/vswitchd.pid) "$@"
+        ;;
        unload)
-               stop restart
+               stop update-modules
                ;;
        status)
                status -p vswitchd.pid vswitchd