Skip to content

Instantly share code, notes, and snippets.

@vy
Created May 27, 2012 14:38
Show Gist options
  • Save vy/2814489 to your computer and use it in GitHub Desktop.
Save vy/2814489 to your computer and use it in GitHub Desktop.
"OpenFlow Toolbelt" shell script
#!/bin/bash
__BIN="$0"
__ACT="$1"; shift
__ARG=("$@")
set -e
OVS_HOME="$HOME/usr-openvswitch"
OVS_VERSION="1.4.1"
OVS_SOURCE="$OVS_HOME/src/openvswitch-$OVS_VERSION"
OVS_PID_FILE="$OVS_HOME/var/run/openvswitch/ovsdb-server.pid"
# Shortcut to check function arguments
usage() {
local arguments=$1; shift
local examples=("$@")
echo "Usage: $arguments"
local k=0
for example in "${examples[@]}"; do
if [ $k -eq 0 ]
then echo "Example: $example"
else echo " $example"
fi
let k+=1
done
exit 1
}
# ovs-ofctl accessor
ofctl() {
[ $# -eq 0 ] && usage "<ofctl-args>" \
"show br0" \
"dump-flows br0"
"$OVS_HOME/bin/ovs-ofctl" "$@"
}
# ovs-vsctl accessor
vsctl() {
[ $# -eq 0 ] && usage "<vsctl-args>" \
"add-br br0" \
"del-br br0" \
"add-port br0 eth1" \
"del-port br0 eth1"
"$OVS_HOME/bin/ovs-vsctl" --no-wait "$@"
}
# vswitchd accessor
vswitchd() {
sudo "$OVS_HOME/sbin/ovs-vswitchd" "$@"
}
# Install kernel module
ovs_insmod() {
sudo insmod "$OVS_SOURCE/datapath/linux/openvswitch_mod.ko"
}
# Create a configuration database
db_create() {
mkdir -p "$OVS_HOME/etc/openvswitch"
"$OVS_HOME/bin/ovsdb-tool" create \
"$OVS_HOME/etc/openvswitch/conf.db" \
"$OVS_SOURCE/vswitchd/vswitch.ovsschema"
}
# Start the configuration database server
db_start() {
mkdir -p "$(dirname \"$OVS_PID_FILE\")"
"$OVS_HOME/sbin/ovsdb-server" \
--remote=punix:"$OVS_HOME/var/run/openvswitch/db.sock" \
--remote=db:Open_vSwitch,manager_options \
--pidfile="$OVS_PID_FILE" \
--detach
}
# stop the configuration database server
db_stop() {
kill $(cat "$OVS_PID_FILE")
}
# Get controller
get_controller() {
[ $# -ne 1 ] && usage "get_controller <dp>" \
"get_controller br0"
local dp="$1"
"$OVS_HOME/bin/ovs-vsctl" --no-wait get-controller "$dp"
}
# Set controller
set_controller() {
[ $# -ne 2 ] && usage "set_controller <dp> <controller>" \
"set_controller br0 tcp:192.168.1.1:6633"
local dp="$1"
local controller="$2"
"$OVS_HOME/bin/ovs-vsctl" --no-wait set-controller "$dp" "$controller"
}
# Delete controller
del_controller() {
[ $# -ne 1 ] && usage "del_controller <dp>" "del_controller br0"
local dp="$1"
"$OVS_HOME/bin/ovs-vsctl" --no-wait del-controller "$dp"
}
# Get datapath id
get_dpid() {
[ $# -ne 1 ] && usage "get_dpid <dp>" "get_dpid br0"
local dp="$1"
"$OVS_HOME/bin/ovs-vsctl" --no-wait get bridge "$dp" \
other-config:datapath-id
}
# Set datapath id
set_dpid() {
[ $# -ne 2 ] && usage "set_dpid <dp> <id>" "set_dpid br0 1"
local dp="$1"
local id=$(printf "%016d" "$2")
"$OVS_HOME/bin/ovs-vsctl" --no-wait set bridge "$dp" \
other-config:datapath-id=$id
}
# Get STP
get_stp() {
[ $# -ne 1 ] && usage "<dp>" "br0"
local dp="$1"
"$OVS_HOME/bin/ovs-vsctl" --no-wait get bridge "$dp" stp_enable
}
# Enable/Disable STP
set_stp() {
[ $# -ne 2 ] && usage "<dp> <true|false>" "br0 true" "br0 false"
local dp=$1
local flag=$2
"$OVS_HOME/bin/ovs-vsctl" --no-wait set bridge "$dp" stp_enable="$flag"
}
# Download OVS
dl_ovs() {
wget http://openvswitch.org/releases/openvswitch-$OVS_VERSION.tar.gz
}
# Install OVS
install_ovs() {
[ $# -lt 1 ] && usage "<tarball> <configure-args>" \
"openvswitch-1.4.1.tar.gz --with-linux=~/kernel/linux-3.3.2"
local tarball="$1"; shift
local configureArgs=("$@")
tar -zxf "$tarball"
cd $(basename "$tarball" .tar.gz)
mkdir -p "$OVS_HOME"
./configure --prefix="$OVS_HOME" "${configureArgs[@]}"
make -j 2
make install
}
# ethtool tips
ethtool_help() {
cat <<EOF
ethtool eth0
ethtool -s eth0 speed 10 duplex full autoneg off
ethtool -s eth0 autoneg on
ethtool -s eth0 autoneg on adver 0x001 # 10 Half
0x002 # 10 Full
0x004 # 100 Half
0x008 # 100 Full
0x010 # 1000 Half(not supported by IEEE standards)
0x020 # 1000 Full
0x8000 # 2500 Full(not supported by IEEE standards)
0x1000 # 10000 Full
0x03F # Auto
EOF
}
# arping tips
arping_help() {
cat <<EOF
arping 192.168.1.1
arping 00:24:54:b9:1c:f8
arping -S 192.168.1.2 192.168.1.1
EOF
}
# Script help
help() {
cat <<EOF
Usage: `basename "$__BIN"` <ACTION>
Actions: ofctl
vsctl
vswitchd
ovs_insmod
db_create
db_start
get_controller
set_controller
del_controller
get_dpid
set_dpid
get_stp
set_stp
dl_ovs
install_ovs
ethtool_help
arping_help
EOF
exit 1
}
case "$__ACT" in
ofctl|vsctl|vswitchd|ovs_insmod|\
db_create|db_start|db_stop|\
get_controller|set_controller|del_controller|\
get_dpid|set_dpid|\
get_stp|set_stp|\
dl_ovs|install_ovs|\
ethtool_help|arping_help) "$__ACT" "${__ARG[@]}";;
*) help "${__ARG[@]}";;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment