First commit
diff --git a/util/build-ovs-packages.sh b/util/build-ovs-packages.sh
new file mode 100644
index 0000000..6a14659
--- /dev/null
+++ b/util/build-ovs-packages.sh
@@ -0,0 +1,94 @@
+#!/bin/bash
+
+# Attempt to build debian packages for OVS
+
+set -e # exit on error
+set -u # exit on undefined variable
+
+kvers=`uname -r`
+ksrc=/lib/modules/$kvers/build
+dist=`lsb_release -is | tr [A-Z] [a-z]`
+release=`lsb_release -rs`
+arch=`uname -m`
+buildsuffix=-2
+if [ "$arch" = "i686" ]; then arch=i386; fi
+if [ "$arch" = "x86_64" ]; then arch=amd64; fi
+
+overs=1.4.0
+ovs=openvswitch-$overs
+ovstgz=$ovs.tar.gz
+ovsurl=http://openvswitch.org/releases/$ovstgz
+
+install='sudo apt-get install -y'
+
+echo "*** Installing debian/ubuntu build system"
+ $install build-essential devscripts ubuntu-dev-tools debhelper dh-make
+ $install diff patch cdbs quilt gnupg fakeroot lintian pbuilder piuparts
+ $install module-assistant
+
+echo "*** Installing OVS dependencies"
+ $install pkg-config gcc make python-dev libssl-dev libtool
+ $install dkms ipsec-tools
+
+echo "*** Installing headers for $kvers"
+ $install linux-headers-$kvers
+
+echo "*** Retrieving OVS source"
+ wget -c $ovsurl
+ tar xzf $ovstgz
+ cd $ovs
+
+echo "*** Patching OVS source"
+ # Not sure why this fails, but off it goes!
+ sed -i -e 's/dh_strip/# dh_strip/' debian/rules
+ if [ "$release" = "10.04" ]; then
+ # Lucid doesn't seem to have all the packages for ovsdbmonitor
+ echo "*** Patching debian/rules to remove dh_python2"
+ sed -i -e 's/dh_python2/dh_pysupport/' debian/rules
+ echo "*** Not building ovsdbmonitor since it's too hard on 10.04"
+ mv debian/ovsdbmonitor.install debian/ovsdbmonitor.install.backup
+ sed -i -e 's/ovsdbmonitor.install/ovsdbmonitor.install.backup/' Makefile.in
+ else
+ # Install a bag of hurt for ovsdbmonitor
+ $install python-pyside.qtcore pyqt4-dev-tools python-twisted python-twisted-bin \
+ python-twisted-core python-twisted-conch python-anyjson python-zope.interface
+ fi
+ # init script was written to assume that commands complete
+ sed -i -e 's/^set -e/#set -e/' debian/openvswitch-controller.init
+
+echo "*** Building OVS user packages"
+ opts=--with-linux=/lib/modules/`uname -r`/build
+ fakeroot make -f debian/rules DATAPATH_CONFIGURE_OPTS=$opts binary
+
+echo "*** Building OVS datapath kernel module package"
+ # Still looking for the "right" way to do this...
+ sudo mkdir -p /usr/src/linux
+ ln -sf _debian/openvswitch.tar.gz .
+ sudo make -f debian/rules.modules KSRC=$ksrc KVERS=$kvers binary-modules
+
+echo "*** Built the following packages:"
+ cd ~
+ ls -l *deb
+
+archive=ovs-$overs-core-$dist-$release-$arch$buildsuffix.tar
+ovsbase='common pki switch brcompat controller datapath-dkms'
+echo "*** Packing up $ovsbase .debs into:"
+echo " $archive"
+ pkgs=""
+ for component in $ovsbase; do
+ if echo $component | egrep 'dkms|pki'; then
+ # Architecture-independent packages
+ deb=(openvswitch-${component}_$overs*all.deb)
+ else
+ deb=(openvswitch-${component}_$overs*$arch.deb)
+ fi
+ pkgs="$pkgs $deb"
+ done
+ rm -rf $archive
+ tar cf $archive $pkgs
+
+echo "*** Contents of archive $archive:"
+ tar tf $archive
+
+echo "*** Done (hopefully)"
+
diff --git a/util/colorfilters b/util/colorfilters
new file mode 100644
index 0000000..745fe21
--- /dev/null
+++ b/util/colorfilters
@@ -0,0 +1,21 @@
+# DO NOT EDIT THIS FILE! It was created by Wireshark
+@Bad TCP@tcp.analysis.flags@[0,0,0][65535,24383,24383]
+@HSRP State Change@hsrp.state != 8 && hsrp.state != 16@[0,0,0][65535,63222,0]
+@Spanning Tree Topology Change@stp.type == 0x80@[0,0,0][65535,63222,0]
+@OSPF State Change@ospf.msg != 1@[0,0,0][65535,63222,0]
+@ICMP errors@icmp.type eq 3 || icmp.type eq 4 || icmp.type eq 5 || icmp.type eq 11@[0,0,0][0,65535,3616]
+@ARP@arp@[55011,59486,65534][0,0,0]
+@ICMP@icmp@[49680,49737,65535][0,0,0]
+@TCP RST@tcp.flags.reset eq 1@[37008,0,0][65535,63121,32911]
+@TTL low or unexpected@( ! ip.dst == 224.0.0.0/4 && ip.ttl < 5) || (ip.dst == 224.0.0.0/24 && ip.ttl != 1)@[37008,0,0][65535,65535,65535]
+@of@of@[0,5,65535][65535,65535,65535]
+@Checksum Errors@cdp.checksum_bad==1 || edp.checksum_bad==1 || ip.checksum_bad==1 || tcp.checksum_bad==1 || udp.checksum_bad==1@[0,0,0][65535,24383,24383]
+@SMB@smb || nbss || nbns || nbipx || ipxsap || netbios@[65534,64008,39339][0,0,0]
+@HTTP@http || tcp.port == 80@[36107,65535,32590][0,0,0]
+@IPX@ipx || spx@[65534,58325,58808][0,0,0]
+@DCERPC@dcerpc@[51199,38706,65533][0,0,0]
+@Routing@hsrp || eigrp || ospf || bgp || cdp || vrrp || gvrp || igmp || ismp@[65534,62325,54808][0,0,0]
+@TCP SYN/FIN@tcp.flags & 0x02 || tcp.flags.fin == 1@[41026,41026,41026][0,0,0]
+@TCP@tcp@[59345,58980,65534][0,0,0]
+@UDP@udp@[28834,57427,65533][0,0,0]
+@Broadcast@eth[0] & 1@[65535,65535,65535][32768,32768,32768]
diff --git a/util/doxify.py b/util/doxify.py
new file mode 100644
index 0000000..f9f60ad
--- /dev/null
+++ b/util/doxify.py
@@ -0,0 +1,89 @@
+#!/usr/bin/python
+
+"""
+Convert simple documentation to epydoc/pydoctor-compatible markup
+"""
+
+from sys import stdin, stdout, argv
+import os
+from tempfile import mkstemp
+from subprocess import call
+
+import re
+
+spaces = re.compile( r'\s+' )
+singleLineExp = re.compile( r'\s+"([^"]+)"' )
+commentStartExp = re.compile( r'\s+"""' )
+commentEndExp = re.compile( r'"""$' )
+returnExp = re.compile( r'\s+(returns:.*)' )
+lastindent = ''
+
+
+comment = False
+
+def fixParam( line ):
+ "Change foo: bar to @foo bar"
+ result = re.sub( r'(\w+):', r'@param \1', line )
+ result = re.sub( r' @', r'@', result)
+ return result
+
+def fixReturns( line ):
+ "Change returns: foo to @return foo"
+ return re.sub( 'returns:', r'@returns', line )
+
+def fixLine( line ):
+ global comment
+ match = spaces.match( line )
+ if not match:
+ return line
+ else:
+ indent = match.group(0)
+ if singleLineExp.match( line ):
+ return re.sub( '"', '"""', line )
+ if commentStartExp.match( line ):
+ comment = True
+ if comment:
+ line = fixReturns( line )
+ line = fixParam( line )
+ if commentEndExp.search( line ):
+ comment = False
+ return line
+
+
+def test():
+ "Test transformations"
+ assert fixLine(' "foo"') == ' """foo"""'
+ assert fixParam( 'foo: bar' ) == '@param foo bar'
+ assert commentStartExp.match( ' """foo"""')
+
+def funTest():
+ testFun = (
+ 'def foo():\n'
+ ' "Single line comment"\n'
+ ' """This is a test"""\n'
+ ' bar: int\n'
+ ' baz: string\n'
+ ' returns: junk"""\n'
+ ' if True:\n'
+ ' print "OK"\n'
+ ).splitlines( True )
+
+ fixLines( testFun )
+
+def fixLines( lines, fid ):
+ for line in lines:
+ os.write( fid, fixLine( line ) )
+
+if __name__ == '__main__':
+ if False:
+ funTest()
+ infile = open( argv[1] )
+ outfid, outname = mkstemp()
+ fixLines( infile.readlines(), outfid )
+ infile.close()
+ os.close( outfid )
+ call( [ 'doxypy', outname ] )
+
+
+
+
diff --git a/util/install.sh b/util/install.sh
new file mode 100644
index 0000000..8bd7cac
--- /dev/null
+++ b/util/install.sh
@@ -0,0 +1,562 @@
+#!/usr/bin/env bash
+
+# Mininet install script for Ubuntu (and Debian Lenny)
+# Brandon Heller (brandonh@stanford.edu)
+
+# Fail on error
+set -e
+
+# Fail on unset var usage
+set -o nounset
+
+# Location of CONFIG_NET_NS-enabled kernel(s)
+KERNEL_LOC=http://www.openflow.org/downloads/mininet
+
+# Attempt to identify Linux release
+
+DIST=Unknown
+RELEASE=Unknown
+CODENAME=Unknown
+ARCH=`uname -m`
+if [ "$ARCH" = "x86_64" ]; then ARCH="amd64"; fi
+if [ "$ARCH" = "i686" ]; then ARCH="i386"; fi
+
+test -e /etc/debian_version && DIST="Debian"
+grep Ubuntu /etc/lsb-release &> /dev/null && DIST="Ubuntu"
+if [ "$DIST" = "Ubuntu" ] || [ "$DIST" = "Debian" ]; then
+ install='sudo apt-get -y install'
+ remove='sudo apt-get -y remove'
+ pkginst='sudo dpkg -i'
+ # Prereqs for this script
+ if ! which lsb_release &> /dev/null; then
+ $install lsb-release
+ fi
+ if ! which bc &> /dev/null; then
+ $install bc
+ fi
+fi
+if which lsb_release &> /dev/null; then
+ DIST=`lsb_release -is`
+ RELEASE=`lsb_release -rs`
+ CODENAME=`lsb_release -cs`
+fi
+echo "Detected Linux distribution: $DIST $RELEASE $CODENAME $ARCH"
+
+# Kernel params
+
+if [ "$DIST" = "Ubuntu" ]; then
+ if [ "$RELEASE" = "10.04" ]; then
+ KERNEL_NAME='3.0.0-15-generic'
+ else
+ KERNEL_NAME=`uname -r`
+ fi
+ KERNEL_HEADERS=linux-headers-${KERNEL_NAME}
+elif [ "$DIST" = "Debian" ] && [ "$ARCH" = "i386" ] && [ "$CODENAME" = "lenny" ]; then
+ KERNEL_NAME=2.6.33.1-mininet
+ KERNEL_HEADERS=linux-headers-${KERNEL_NAME}_${KERNEL_NAME}-10.00.Custom_i386.deb
+ KERNEL_IMAGE=linux-image-${KERNEL_NAME}_${KERNEL_NAME}-10.00.Custom_i386.deb
+else
+ echo "Install.sh currently only supports Ubuntu and Debian Lenny i386."
+ exit 1
+fi
+
+# More distribution info
+DIST_LC=`echo $DIST | tr [A-Z] [a-z]` # as lower case
+
+# Kernel Deb pkg to be removed:
+KERNEL_IMAGE_OLD=linux-image-2.6.26-33-generic
+
+DRIVERS_DIR=/lib/modules/${KERNEL_NAME}/kernel/drivers/net
+
+OVS_RELEASE=1.4.0
+OVS_PACKAGE_LOC=https://github.com/downloads/mininet/mininet
+OVS_BUILDSUFFIX=-ignore # was -2
+OVS_PACKAGE_NAME=ovs-$OVS_RELEASE-core-$DIST_LC-$RELEASE-$ARCH$OVS_BUILDSUFFIX.tar
+OVS_SRC=~/openvswitch
+OVS_TAG=v$OVS_RELEASE
+OVS_BUILD=$OVS_SRC/build-$KERNEL_NAME
+OVS_KMODS=($OVS_BUILD/datapath/linux/{openvswitch_mod.ko,brcompat_mod.ko})
+
+function kernel {
+ echo "Install Mininet-compatible kernel if necessary"
+ sudo apt-get update
+ if [ "$DIST" = "Ubuntu" ] && [ "$RELEASE" = "10.04" ]; then
+ $install linux-image-$KERNEL_NAME
+ elif [ "$DIST" = "Debian" ]; then
+ # The easy approach: download pre-built linux-image and linux-headers packages:
+ wget -c $KERNEL_LOC/$KERNEL_HEADERS
+ wget -c $KERNEL_LOC/$KERNEL_IMAGE
+
+ # Install custom linux headers and image:
+ $pkginst $KERNEL_IMAGE $KERNEL_HEADERS
+
+ # The next two steps are to work around a bug in newer versions of
+ # kernel-package, which fails to add initrd images with the latest kernels.
+ # See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=525032
+ # Generate initrd image if the .deb didn't install it:
+ if ! test -e /boot/initrd.img-${KERNEL_NAME}; then
+ sudo update-initramfs -c -k ${KERNEL_NAME}
+ fi
+
+ # Ensure /boot/grub/menu.lst boots with initrd image:
+ sudo update-grub
+
+ # The default should be the new kernel. Otherwise, you may need to modify
+ # /boot/grub/menu.lst to set the default to the entry corresponding to the
+ # kernel you just installed.
+ fi
+}
+
+function kernel_clean {
+ echo "Cleaning kernel..."
+
+ # To save disk space, remove previous kernel
+ if ! $remove $KERNEL_IMAGE_OLD; then
+ echo $KERNEL_IMAGE_OLD not installed.
+ fi
+
+ # Also remove downloaded packages:
+ rm -f ~/linux-headers-* ~/linux-image-*
+}
+
+# Install Mininet deps
+function mn_deps {
+ echo "Installing Mininet dependencies"
+ $install gcc make screen psmisc xterm ssh iperf iproute telnet \
+ python-setuptools python-networkx cgroup-bin ethtool help2man \
+ pyflakes pylint pep8
+
+ if [ "$DIST" = "Ubuntu" ] && [ "$RELEASE" = "10.04" ]; then
+ echo "Upgrading networkx to avoid deprecation warning"
+ sudo easy_install --upgrade networkx
+ fi
+
+ # Add sysctl parameters as noted in the INSTALL file to increase kernel
+ # limits to support larger setups:
+ sudo su -c "cat $HOME/mininet/util/sysctl_addon >> /etc/sysctl.conf"
+
+ # Load new sysctl settings:
+ sudo sysctl -p
+
+ echo "Installing Mininet core"
+ pushd ~/mininet
+ sudo make install
+ popd
+}
+
+# The following will cause a full OF install, covering:
+# -user switch
+# The instructions below are an abbreviated version from
+# http://www.openflowswitch.org/wk/index.php/Debian_Install
+# ... modified to use Debian Lenny rather than unstable.
+function of {
+ echo "Installing OpenFlow reference implementation..."
+ cd ~/
+ $install git-core autoconf automake autotools-dev pkg-config \
+ make gcc libtool libc6-dev
+ git clone git://openflowswitch.org/openflow.git
+ cd ~/openflow
+
+ # Patch controller to handle more than 16 switches
+ patch -p1 < ~/mininet/util/openflow-patches/controller.patch
+
+ # Resume the install:
+ ./boot.sh
+ ./configure
+ make
+ sudo make install
+
+ # Remove avahi-daemon, which may cause unwanted discovery packets to be
+ # sent during tests, near link status changes:
+ $remove avahi-daemon
+
+ # Disable IPv6. Add to /etc/modprobe.d/blacklist:
+ if [ "$DIST" = "Ubuntu" ]; then
+ BLACKLIST=/etc/modprobe.d/blacklist.conf
+ else
+ BLACKLIST=/etc/modprobe.d/blacklist
+ fi
+ sudo sh -c "echo 'blacklist net-pf-10\nblacklist ipv6' >> $BLACKLIST"
+ cd ~
+}
+
+function wireshark {
+ echo "Installing Wireshark dissector..."
+
+ sudo apt-get install -y wireshark libgtk2.0-dev
+
+ if [ "$DIST" = "Ubuntu" ] && [ "$RELEASE" != "10.04" ]; then
+ # Install newer version
+ sudo apt-get install -y scons mercurial libglib2.0-dev
+ sudo apt-get install -y libwiretap-dev libwireshark-dev
+ cd ~
+ hg clone https://bitbucket.org/barnstorm/of-dissector
+ cd of-dissector/src
+ export WIRESHARK=/usr/include/wireshark
+ scons
+ # libwireshark0/ on 11.04; libwireshark1/ on later
+ WSDIR=`ls -d /usr/lib/wireshark/libwireshark* | head -1`
+ WSPLUGDIR=$WSDIR/plugins/
+ sudo cp openflow.so $WSPLUGDIR
+ echo "Copied openflow plugin to $WSPLUGDIR"
+ else
+ # Install older version from reference source
+ cd ~/openflow/utilities/wireshark_dissectors/openflow
+ make
+ sudo make install
+ fi
+
+ # Copy coloring rules: OF is white-on-blue:
+ mkdir -p ~/.wireshark
+ cp ~/mininet/util/colorfilters ~/.wireshark
+}
+
+
+# Install Open vSwitch
+# Instructions derived from OVS INSTALL, INSTALL.OpenFlow and README files.
+
+function ovs {
+ echo "Installing Open vSwitch..."
+
+ # Required for module build/dkms install
+ $install $KERNEL_HEADERS
+
+ ovspresent=0
+
+ # First see if we have packages
+ # XXX wget -c seems to fail from github/amazon s3
+ cd /tmp
+ if wget $OVS_PACKAGE_LOC/$OVS_PACKAGE_NAME 2> /dev/null; then
+ $install patch dkms fakeroot python-argparse
+ tar xf $OVS_PACKAGE_NAME
+ orig=`tar tf $OVS_PACKAGE_NAME`
+ # Now install packages in reasonable dependency order
+ order='dkms common pki openvswitch-switch brcompat controller'
+ pkgs=""
+ for p in $order; do
+ pkg=`echo "$orig" | grep $p`
+ # Annoyingly, things seem to be missing without this flag
+ $pkginst --force-confmiss $pkg
+ done
+ ovspresent=1
+ fi
+
+ # Otherwise try distribution's OVS packages
+ if [ "$DIST" = "Ubuntu" ] && [ `expr $RELEASE '>=' 11.10` = 1 ]; then
+ if ! dpkg --get-selections | grep openvswitch-datapath; then
+ # If you've already installed a datapath, assume you
+ # know what you're doing and don't need dkms datapath.
+ # Otherwise, install it.
+ $install openvswitch-datapath-dkms
+ fi
+ if $install openvswitch-switch openvswitch-controller; then
+ echo "Ignoring error installing openvswitch-controller"
+ fi
+ ovspresent=1
+ fi
+
+ # Switch can run on its own, but
+ # Mininet should control the controller
+ if [ -e /etc/init.d/openvswitch-controller ]; then
+ if sudo service openvswitch-controller stop; then
+ echo "Stopped running controller"
+ fi
+ sudo update-rc.d openvswitch-controller disable
+ fi
+
+ if [ $ovspresent = 1 ]; then
+ echo "Done (hopefully) installing packages"
+ cd ~
+ return
+ fi
+
+ # Otherwise attempt to install from source
+
+ $install pkg-config gcc make python-dev libssl-dev libtool
+
+ if [ "$DIST" = "Debian" ]; then
+ if [ "$CODENAME" = "lenny" ]; then
+ $install git-core
+ # Install Autoconf 2.63+ backport from Debian Backports repo:
+ # Instructions from http://backports.org/dokuwiki/doku.php?id=instructions
+ sudo su -c "echo 'deb http://www.backports.org/debian lenny-backports main contrib non-free' >> /etc/apt/sources.list"
+ sudo apt-get update
+ sudo apt-get -y --force-yes install debian-backports-keyring
+ sudo apt-get -y --force-yes -t lenny-backports install autoconf
+ fi
+ else
+ $install git
+ fi
+
+ # Install OVS from release
+ cd ~/
+ git clone git://openvswitch.org/openvswitch $OVS_SRC
+ cd $OVS_SRC
+ git checkout $OVS_TAG
+ ./boot.sh
+ BUILDDIR=/lib/modules/${KERNEL_NAME}/build
+ if [ ! -e $BUILDDIR ]; then
+ echo "Creating build sdirectory $BUILDDIR"
+ sudo mkdir -p $BUILDDIR
+ fi
+ opts="--with-linux=$BUILDDIR"
+ mkdir -p $OVS_BUILD
+ cd $OVS_BUILD
+ ../configure $opts
+ make
+ sudo make install
+
+ modprobe
+}
+
+function remove_ovs {
+ pkgs=`dpkg --get-selections | grep openvswitch | awk '{ print $1;}'`
+ echo "Removing existing Open vSwitch packages:"
+ echo $pkgs
+ if ! $remove $pkgs; then
+ echo "Not all packages removed correctly"
+ fi
+ # For some reason this doesn't happen
+ if scripts=`ls /etc/init.d/*openvswitch* 2>/dev/null`; then
+ echo $scripts
+ for s in $scripts; do
+ s=$(basename $s)
+ echo SCRIPT $s
+ sudo service $s stop
+ sudo rm -f /etc/init.d/$s
+ sudo update-rc.d -f $s remove
+ done
+ fi
+ echo "Done removing OVS"
+}
+
+# Install NOX with tutorial files
+function nox {
+ echo "Installing NOX w/tutorial files..."
+
+ # Install NOX deps:
+ $install autoconf automake g++ libtool python python-twisted \
+ swig libssl-dev make
+ if [ "$DIST" = "Debian" ]; then
+ $install libboost1.35-dev
+ elif [ "$DIST" = "Ubuntu" ]; then
+ $install python-dev libboost-dev
+ $install libboost-filesystem-dev
+ $install libboost-test-dev
+ fi
+ # Install NOX optional deps:
+ $install libsqlite3-dev python-simplejson
+
+ # Fetch NOX destiny
+ cd ~/
+ git clone https://github.com/noxrepo/nox-classic.git noxcore
+ cd noxcore
+ if ! git checkout -b destiny remotes/origin/destiny ; then
+ echo "Did not check out a new destiny branch - assuming current branch is destiny"
+ fi
+
+ # Apply patches
+ git checkout -b tutorial-destiny
+ git am ~/mininet/util/nox-patches/*tutorial-port-nox-destiny*.patch
+ if [ "$DIST" = "Ubuntu" ] && [ `expr $RELEASE '>=' 12.04` = 1 ]; then
+ git am ~/mininet/util/nox-patches/*nox-ubuntu12-hacks.patch
+ fi
+
+ # Build
+ ./boot.sh
+ mkdir build
+ cd build
+ ../configure
+ make -j3
+ #make check
+
+ # Add NOX_CORE_DIR env var:
+ sed -i -e 's|# for examples$|&\nexport NOX_CORE_DIR=~/noxcore/build/src|' ~/.bashrc
+
+ # To verify this install:
+ #cd ~/noxcore/build/src
+ #./nox_core -v -i ptcp:
+}
+
+# "Install" POX
+function pox {
+ echo "Installing POX into $HOME/pox..."
+ cd ~
+ git clone https://github.com/noxrepo/pox.git
+}
+
+# Install OFtest
+function oftest {
+ echo "Installing oftest..."
+
+ # Install deps:
+ $install tcpdump python-scapy
+
+ # Install oftest:
+ cd ~/
+ git clone git://github.com/floodlight/oftest
+ cd oftest
+ cd tools/munger
+ sudo make install
+}
+
+# Install cbench
+function cbench {
+ echo "Installing cbench..."
+
+ $install libsnmp-dev libpcap-dev libconfig-dev
+ cd ~/
+ git clone git://openflow.org/oflops.git
+ cd oflops
+ sh boot.sh || true # possible error in autoreconf, so run twice
+ sh boot.sh
+ ./configure --with-openflow-src-dir=$HOME/openflow
+ make
+ sudo make install || true # make install fails; force past this
+}
+
+function other {
+ echo "Doing other setup tasks..."
+
+ # Enable command auto completion using sudo; modify ~/.bashrc:
+ sed -i -e 's|# for examples$|&\ncomplete -cf sudo|' ~/.bashrc
+
+ # Install tcpdump and tshark, cmd-line packet dump tools. Also install gitk,
+ # a graphical git history viewer.
+ $install tcpdump tshark gitk
+
+ # Install common text editors
+ $install vim nano emacs
+
+ # Install NTP
+ $install ntp
+
+ # Set git to colorize everything.
+ git config --global color.diff auto
+ git config --global color.status auto
+ git config --global color.branch auto
+
+ # Reduce boot screen opt-out delay. Modify timeout in /boot/grub/menu.lst to 1:
+ if [ "$DIST" = "Debian" ]; then
+ sudo sed -i -e 's/^timeout.*$/timeout 1/' /boot/grub/menu.lst
+ fi
+
+ # Clean unneeded debs:
+ rm -f ~/linux-headers-* ~/linux-image-*
+}
+
+# Script to copy built OVS kernel module to where modprobe will
+# find them automatically. Removes the need to keep an environment variable
+# for insmod usage, and works nicely with multiple kernel versions.
+#
+# The downside is that after each recompilation of OVS you'll need to
+# re-run this script. If you're using only one kernel version, then it may be
+# a good idea to use a symbolic link in place of the copy below.
+function modprobe {
+ echo "Setting up modprobe for OVS kmod..."
+
+ sudo cp $OVS_KMODS $DRIVERS_DIR
+ sudo depmod -a ${KERNEL_NAME}
+}
+
+function all {
+ echo "Running all commands..."
+ kernel
+ mn_deps
+ of
+ wireshark
+ ovs
+ # NOX-classic is deprecated, but you can install it manually if desired.
+ # nox
+ pox
+ oftest
+ cbench
+ other
+ echo "Please reboot, then run ./mininet/util/install.sh -c to remove unneeded packages."
+ echo "Enjoy Mininet!"
+}
+
+# Restore disk space and remove sensitive files before shipping a VM.
+function vm_clean {
+ echo "Cleaning VM..."
+ sudo apt-get clean
+ sudo rm -rf /tmp/*
+ sudo rm -rf openvswitch*.tar.gz
+
+ # Remove sensistive files
+ history -c # note this won't work if you have multiple bash sessions
+ rm -f ~/.bash_history # need to clear in memory and remove on disk
+ rm -f ~/.ssh/id_rsa* ~/.ssh/known_hosts
+ sudo rm -f ~/.ssh/authorized_keys*
+
+ # Remove Mininet files
+ #sudo rm -f /lib/modules/python2.5/site-packages/mininet*
+ #sudo rm -f /usr/bin/mnexec
+
+ # Clear optional dev script for SSH keychain load on boot
+ rm -f ~/.bash_profile
+
+ # Clear git changes
+ git config --global user.name "None"
+ git config --global user.email "None"
+
+ # Remove mininet install script
+ rm -f install-mininet.sh
+}
+
+function usage {
+ printf 'Usage: %s [-acdfhkmntvxy]\n\n' $(basename $0) >&2
+
+ printf 'This install script attempts to install useful packages\n' >&2
+ printf 'for Mininet. It should (hopefully) work on Ubuntu 10.04, 11.10\n' >&2
+ printf 'and Debian 5.0 (Lenny). If you run into trouble, try\n' >&2
+ printf 'installing one thing at a time, and looking at the \n' >&2
+ printf 'specific installation function in this script.\n\n' >&2
+
+ printf 'options:\n' >&2
+ printf -- ' -a: (default) install (A)ll packages - good luck!\n' >&2
+ printf -- ' -b: install controller (B)enchmark (oflops)\n' >&2
+ printf -- ' -c: (C)lean up after kernel install\n' >&2
+ printf -- ' -d: (D)elete some sensitive files from a VM image\n' >&2
+ printf -- ' -f: install open(F)low\n' >&2
+ printf -- ' -h: print this (H)elp message\n' >&2
+ printf -- ' -k: install new (K)ernel\n' >&2
+ printf -- ' -m: install Open vSwitch kernel (M)odule from source dir\n' >&2
+ printf -- ' -n: install mini(N)et dependencies + core files\n' >&2
+ printf -- ' -r: remove existing Open vSwitch packages\n' >&2
+ printf -- ' -t: install o(T)her stuff\n' >&2
+ printf -- ' -v: install open (V)switch\n' >&2
+ printf -- ' -w: install OpenFlow (w)ireshark dissector\n' >&2
+ printf -- ' -x: install NO(X) OpenFlow controller\n' >&2
+ printf -- ' -y: install (A)ll packages\n' >&2
+
+ exit 2
+}
+
+if [ $# -eq 0 ]
+then
+ all
+else
+ while getopts 'abcdfhkmnprtvwx' OPTION
+ do
+ case $OPTION in
+ a) all;;
+ b) cbench;;
+ c) kernel_clean;;
+ d) vm_clean;;
+ f) of;;
+ h) usage;;
+ k) kernel;;
+ m) modprobe;;
+ n) mn_deps;;
+ p) pox;;
+ r) remove_ovs;;
+ t) other;;
+ v) ovs;;
+ w) wireshark;;
+ x) nox;;
+ ?) usage;;
+ esac
+ done
+ shift $(($OPTIND - 1))
+fi
diff --git a/util/kbuild/kbuild b/util/kbuild/kbuild
new file mode 100644
index 0000000..c8d2f96
--- /dev/null
+++ b/util/kbuild/kbuild
@@ -0,0 +1,71 @@
+#!/bin/bash
+
+# Script to build new Debian kernel packages for 2.6.33.1
+#
+# Caveats:
+#
+# Since kernel-package in debian-stable doesn't work with
+# 2.6.33.1, we attempt to patch it in place. This may not be the
+# right thing to do. A possibly better alternative is to install
+# a later version of kernel-package, although that could potentially
+# cause problems with upgrades, etc..
+#
+# The patch to tun.c is a workaround rather than a real fix.
+#
+# Building a full Debian kernel package with all drivers takes a long
+# time, 60-80 minutes on my laptop.
+#
+# Re-running a make-kpkg may not work without running 'make-kpkg clean'
+
+# Season to taste
+# export PATH=/usr/lib/ccache:$PATH
+export CONCURRENCY_LEVEL=3
+
+debversion=2.6.26-2-686-bigmem
+
+image=linux-image-$debversion
+
+echo "*** Installing $image"
+sudo aptitude install $image
+
+newversion=2.6.33.1
+archive=linux-$newversion.tar.bz2
+location=http://www.kernel.org/pub/linux/kernel/v2.6
+
+echo "*** Fetching $location/$archive"
+wget -c $location/$archive
+
+tree=linux-$newversion
+if [ -e $tree ]; then
+ echo "*** $tree already exists"
+else
+ echo "*** Extracting $archive"
+ tar xjf $archive
+fi
+
+echo "*** Patching tun driver"
+patch $tree/drivers/net/tun.c < tun.patch
+
+echo "*** Patching debian build script"
+sudo patch /usr/share/kernel-package/ruleset/misc/version_vars.mk < version_vars.patch
+
+config=/boot/config-$debversion
+echo "*** Copying $config to $tree/.config"
+cp $config $tree/.config
+
+echo "*** Updating config"
+cd $tree
+yes '' | make oldconfig 1> /dev/null
+sed 's/# CONFIG_NET_NS is not set/CONFIG_NET_NS=y/' .config > .config-new
+mv .config-new .config
+echo "*** Result: " `grep CONFIG_NET_NS .config`
+
+echo "*** Building kernel"
+time fakeroot make-kpkg --initrd --append-to-version=-mininet kernel_image kernel_headers
+
+cd ..
+echo "*** Done - package should be in current directory"
+ls *$newversion*.deb
+
+echo "To install:"
+echo "# dpkg -i " *$newversion*.deb
diff --git a/util/kbuild/tun.patch b/util/kbuild/tun.patch
new file mode 100644
index 0000000..3c4cc69
--- /dev/null
+++ b/util/kbuild/tun.patch
@@ -0,0 +1,13 @@
+--- linux-2.6.33.1/drivers/net/tun.c 2010-03-24 22:47:32.000000000 -0700
++++ tun-new.c 2010-03-24 22:45:00.000000000 -0700
+@@ -1006,7 +1006,9 @@
+ if (err < 0)
+ goto err_free_sk;
+
+- if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
++ /* BL hack: check for null parent kobj */
++ if (!tun->dev->dev.kobj.sd ||
++ device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
+ device_create_file(&tun->dev->dev, &dev_attr_owner) ||
+ device_create_file(&tun->dev->dev, &dev_attr_group))
+ printk(KERN_ERR "Failed to create tun sysfs files\n");
diff --git a/util/kbuild/version_vars.patch b/util/kbuild/version_vars.patch
new file mode 100644
index 0000000..6f55901
--- /dev/null
+++ b/util/kbuild/version_vars.patch
@@ -0,0 +1,18 @@
+--- /usr/share/kernel-package/ruleset/misc/version_vars.mk 2010-03-25 18:14:41.000000000 -0700
++++ version_vars.mk 2010-03-03 06:46:59.000000000 -0800
+@@ -138,11 +138,13 @@
+ EXTRAV_ARG :=
+ endif
+
+-UTS_RELEASE_HEADER=$(call doit,if [ -f include/linux/utsrelease.h ]; then \
++UTS_RELEASE_HEADER=$(call doit, if [ -f include/generated/utsrelease.h ]; then \
++ echo include/generated/utsrelease.h; \
++ else if [ -f include/linux/utsrelease.h ]; then \
+ echo include/linux/utsrelease.h; \
+ else \
+ echo include/linux/version.h ; \
+- fi)
++ fi fi)
+ UTS_RELEASE_VERSION=$(call doit,if [ -f $(UTS_RELEASE_HEADER) ]; then \
+ grep 'define UTS_RELEASE' $(UTS_RELEASE_HEADER) | \
+ perl -nle 'm/^\s*\#define\s+UTS_RELEASE\s+("?)(\S+)\1/g && print $$2;';\
diff --git a/util/nox-patches/0001-OpenFlow-tutorial-port-nox-destiny.patch b/util/nox-patches/0001-OpenFlow-tutorial-port-nox-destiny.patch
new file mode 100644
index 0000000..881fc55
--- /dev/null
+++ b/util/nox-patches/0001-OpenFlow-tutorial-port-nox-destiny.patch
@@ -0,0 +1,298 @@
+From 5c9610ffb88c89b0f36359ad3c7547831482a3ff Mon Sep 17 00:00:00 2001
+From: Bob Lantz <rlantz@cs.stanford.edu>
+Date: Fri, 3 Feb 2012 14:48:58 -0800
+Subject: [PATCH] OpenFlow tutorial port nox-destiny.
+
+---
+ src/nox/coreapps/examples/Makefile.am | 2 +-
+ src/nox/coreapps/examples/tutorial/Makefile.am | 25 ++++
+ src/nox/coreapps/examples/tutorial/meta.json | 12 ++
+ src/nox/coreapps/examples/tutorial/pytutorial.py | 67 +++++++++++
+ src/nox/coreapps/examples/tutorial/tutorial.cc | 134 ++++++++++++++++++++++
+ 5 files changed, 239 insertions(+), 1 deletions(-)
+ create mode 100644 src/nox/coreapps/examples/tutorial/Makefile.am
+ create mode 100644 src/nox/coreapps/examples/tutorial/__init__.py
+ create mode 100644 src/nox/coreapps/examples/tutorial/meta.json
+ create mode 100644 src/nox/coreapps/examples/tutorial/pytutorial.py
+ create mode 100644 src/nox/coreapps/examples/tutorial/tutorial.cc
+
+diff --git a/src/nox/coreapps/examples/Makefile.am b/src/nox/coreapps/examples/Makefile.am
+index 126f32e..1a0458c 100644
+--- a/src/nox/coreapps/examples/Makefile.am
++++ b/src/nox/coreapps/examples/Makefile.am
+@@ -1,6 +1,6 @@
+ include ../../../Make.vars
+
+-SUBDIRS = t
++SUBDIRS = tutorial t
+
+ EXTRA_DIST =\
+ meta.json\
+diff --git a/src/nox/coreapps/examples/tutorial/Makefile.am b/src/nox/coreapps/examples/tutorial/Makefile.am
+new file mode 100644
+index 0000000..51cf921
+--- /dev/null
++++ b/src/nox/coreapps/examples/tutorial/Makefile.am
+@@ -0,0 +1,25 @@
++include ../../../../Make.vars
++
++EXTRA_DIST =\
++ meta.xml \
++ __init__.py \
++ pytutorial.py
++
++if PY_ENABLED
++AM_CPPFLAGS += $(PYTHON_CPPFLAGS)
++endif # PY_ENABLED
++
++pkglib_LTLIBRARIES = \
++ tutorial.la
++
++tutorial_la_CPPFLAGS = $(AM_CPPFLAGS) -I $(top_srcdir)/src/nox -I $(top_srcdir)/src/nox/coreapps/
++tutorial_la_SOURCES = tutorial.cc
++tutorial_la_LDFLAGS = -module -export-dynamic
++
++NOX_RUNTIMEFILES = meta.json \
++ __init__.py \
++ pytutorial.py
++
++all-local: nox-all-local
++clean-local: nox-clean-local
++install-exec-hook: nox-install-local
+diff --git a/src/nox/coreapps/examples/tutorial/__init__.py b/src/nox/coreapps/examples/tutorial/__init__.py
+new file mode 100644
+index 0000000..e69de29
+diff --git a/src/nox/coreapps/examples/tutorial/meta.json b/src/nox/coreapps/examples/tutorial/meta.json
+new file mode 100644
+index 0000000..7a9f227
+--- /dev/null
++++ b/src/nox/coreapps/examples/tutorial/meta.json
+@@ -0,0 +1,12 @@
++{
++ "components": [
++ {
++ "name": "tutorial",
++ "library": "tutorial"
++ },
++ {
++ "name": "pytutorial",
++ "python": "nox.coreapps.examples.tutorial.pytutorial"
++ }
++ ]
++}
+diff --git a/src/nox/coreapps/examples/tutorial/pytutorial.py b/src/nox/coreapps/examples/tutorial/pytutorial.py
+new file mode 100644
+index 0000000..1e21c0b
+--- /dev/null
++++ b/src/nox/coreapps/examples/tutorial/pytutorial.py
+@@ -0,0 +1,67 @@
++# Tutorial Controller
++# Starts as a hub, and your job is to turn this into a learning switch.
++
++import logging
++
++from nox.lib.core import *
++import nox.lib.openflow as openflow
++from nox.lib.packet.ethernet import ethernet
++from nox.lib.packet.packet_utils import mac_to_str, mac_to_int
++
++log = logging.getLogger('nox.coreapps.tutorial.pytutorial')
++
++
++class pytutorial(Component):
++
++ def __init__(self, ctxt):
++ Component.__init__(self, ctxt)
++ # Use this table to store MAC addresses in the format of your choice;
++ # Functions already imported, including mac_to_str, and mac_to_int,
++ # should prove useful for converting the byte array provided by NOX
++ # for packet MAC destination fields.
++ # This table is initialized to empty when your module starts up.
++ self.mac_to_port = {} # key: MAC addr; value: port
++
++ def learn_and_forward(self, dpid, inport, packet, buf, bufid):
++ """Learn MAC src port mapping, then flood or send unicast."""
++
++ # Initial hub behavior: flood packet out everything but input port.
++ # Comment out the line below when starting the exercise.
++ self.send_openflow(dpid, bufid, buf, openflow.OFPP_FLOOD, inport)
++
++ # Starter psuedocode for learning switch exercise below: you'll need to
++ # replace each pseudocode line with more specific Python code.
++
++ # Learn the port for the source MAC
++ #self.mac_to_port = <fill in>
++ #if (destination MAC of the packet is known):
++ # Send unicast packet to known output port
++ #self.send_openflow( <fill in params> )
++ # Later, only after learning controller works:
++ # push down flow entry and remove the send_openflow command above.
++ #self.install_datapath_flow( <fill in params> )
++ #else:
++ #flood packet out everything but the input port
++ #self.send_openflow(dpid, bufid, buf, openflow.OFPP_FLOOD, inport)
++
++ def packet_in_callback(self, dpid, inport, reason, len, bufid, packet):
++ """Packet-in handler"""
++ if not packet.parsed:
++ log.debug('Ignoring incomplete packet')
++ else:
++ self.learn_and_forward(dpid, inport, packet, packet.arr, bufid)
++
++ return CONTINUE
++
++ def install(self):
++ self.register_for_packet_in(self.packet_in_callback)
++
++ def getInterface(self):
++ return str(pytutorial)
++
++def getFactory():
++ class Factory:
++ def instance(self, ctxt):
++ return pytutorial(ctxt)
++
++ return Factory()
+diff --git a/src/nox/coreapps/examples/tutorial/tutorial.cc b/src/nox/coreapps/examples/tutorial/tutorial.cc
+new file mode 100644
+index 0000000..e7240cc
+--- /dev/null
++++ b/src/nox/coreapps/examples/tutorial/tutorial.cc
+@@ -0,0 +1,134 @@
++#include "component.hh"
++#include "config.h"
++#include "packet-in.hh"
++#include "flow.hh"
++#include "assert.hh"
++#include "netinet++/ethernetaddr.hh"
++#include "netinet++/ethernet.hh"
++#include <boost/shared_array.hpp>
++#include <boost/bind.hpp>
++#ifdef LOG4CXX_ENABLED
++#include <boost/format.hpp>
++#include "log4cxx/logger.h"
++#else
++#include "vlog.hh"
++#endif
++
++using namespace std;
++using namespace vigil;
++using namespace vigil::container;
++
++namespace
++{
++ static Vlog_module lg("tutorial");
++
++ /** Learning switch.
++ */
++ class tutorial
++ : public Component
++ {
++ public:
++ /** Constructor.
++ */
++ tutorial(const Context* c, const json_object* node)
++ : Component(c)
++ { }
++
++ /** Configuration.
++ * Add handler for packet-in event.
++ */
++ void configure(const Configuration*)
++ {
++ register_handler<Packet_in_event>
++ (boost::bind(&tutorial::handle, this, _1));
++ }
++
++ /** Just simply install.
++ */
++ void install()
++ {
++ lg.dbg(" Install called ");
++ }
++
++ /** Function to setup flow.
++ */
++ void setup_flow(Flow& flow, datapathid datapath_id ,
++ uint32_t buffer_id, uint16_t out_port)
++ {
++ ofp_flow_mod* ofm;
++ size_t size = sizeof *ofm + sizeof(ofp_action_output);
++ boost::shared_array<char> raw_of(new char[size]);
++ ofm = (ofp_flow_mod*) raw_of.get();
++
++ ofm->header.version = OFP_VERSION;
++ ofm->header.type = OFPT_FLOW_MOD;
++ ofm->header.length = htons(size);
++ ofm->match.wildcards = htonl(0);
++ ofm->match.in_port = htons(flow.in_port);
++ ofm->match.dl_vlan = flow.dl_vlan;
++ memcpy(ofm->match.dl_src, flow.dl_src.octet, sizeof ofm->match.dl_src);
++ memcpy(ofm->match.dl_dst, flow.dl_dst.octet, sizeof ofm->match.dl_dst);
++ ofm->match.dl_type = flow.dl_type;
++ ofm->match.nw_src = flow.nw_src;
++ ofm->match.nw_dst = flow.nw_dst;
++ ofm->match.nw_proto = flow.nw_proto;
++ ofm->match.tp_src = flow.tp_src;
++ ofm->match.tp_dst = flow.tp_dst;
++ ofm->command = htons(OFPFC_ADD);
++ ofm->buffer_id = htonl(buffer_id);
++ ofm->idle_timeout = htons(5);
++ ofm->hard_timeout = htons(OFP_FLOW_PERMANENT);
++ ofm->priority = htons(OFP_DEFAULT_PRIORITY);
++ ofp_action_output& action = *((ofp_action_output*)ofm->actions);
++ memset(&action, 0, sizeof(ofp_action_output));
++ action.type = htons(OFPAT_OUTPUT);
++ action.len = htons(sizeof(ofp_action_output));
++ action.max_len = htons(0);
++ action.port = htons(out_port);
++ send_openflow_command(datapath_id, &ofm->header, true);
++ }
++
++ /** Function to handle packets.
++ * @param datapath_id datapath id of switch
++ * @param in_port port packet is received
++ * @param buffer_id buffer id of packet
++ * @param source source mac address in host order
++ * @param destination destination mac address in host order
++ */
++ void handle_packet(datapathid datapath_id, uint16_t in_port, uint32_t buffer_id,
++ uint64_t source, uint64_t destination)
++ {
++ send_openflow_packet(datapath_id, buffer_id, OFPP_FLOOD,
++ in_port, true);
++ }
++
++ /** Packet-on handler.
++ */
++ Disposition handle(const Event& e)
++ {
++ const Packet_in_event& pi = assert_cast<const Packet_in_event&>(e);
++ uint32_t buffer_id = pi.buffer_id;
++ Flow flow(pi.in_port, *pi.get_buffer());
++
++ // drop LLDP packets
++ if (flow.dl_type == ethernet::LLDP)
++ return CONTINUE;
++
++ // pass handle of unicast packet, else flood
++ if (!flow.dl_src.is_multicast())
++ handle_packet(pi.datapath_id, pi.in_port, buffer_id,
++ flow.dl_src.hb_long(), flow.dl_dst.hb_long());
++ else
++ send_openflow_packet(pi.datapath_id, buffer_id, OFPP_FLOOD,
++ pi.in_port, true);
++
++ return CONTINUE;
++ }
++
++ private:
++};
++
++REGISTER_COMPONENT(container::Simple_component_factory<tutorial>,
++ tutorial);
++
++} // unnamed namespace
+--
+1.7.5.4
+
diff --git a/util/nox-patches/0002-nox-ubuntu12-hacks.patch b/util/nox-patches/0002-nox-ubuntu12-hacks.patch
new file mode 100644
index 0000000..77619bc
--- /dev/null
+++ b/util/nox-patches/0002-nox-ubuntu12-hacks.patch
@@ -0,0 +1,175 @@
+From 166693d7cb640d4a41251b87e92c52d9c688196b Mon Sep 17 00:00:00 2001
+From: Bob Lantz <rlantz@cs.stanford.edu>
+Date: Mon, 14 May 2012 15:30:44 -0700
+Subject: [PATCH] Hacks to get NOX classic/destiny to compile under Ubuntu
+ 12.04
+
+Thanks to Srinivasu R. Kanduru for the initial patch.
+
+Apologies for the hacks - it is my hope that this will be fixed
+upstream eventually.
+
+---
+ config/ac_pkg_swig.m4 | 7 ++++---
+ src/Make.vars | 2 +-
+ src/nox/coreapps/pyrt/deferredcallback.cc | 2 +-
+ src/nox/coreapps/pyrt/pyglue.cc | 2 +-
+ src/nox/coreapps/pyrt/pyrt.cc | 2 +-
+ src/nox/netapps/authenticator/auth.i | 2 ++
+ src/nox/netapps/authenticator/flow_util.i | 1 +
+ src/nox/netapps/routing/routing.i | 2 ++
+ .../switch_management/pyswitch_management.i | 2 ++
+ src/nox/netapps/tests/tests.cc | 2 +-
+ src/nox/netapps/topology/pytopology.i | 2 ++
+ 11 files changed, 18 insertions(+), 8 deletions(-)
+
+diff --git a/config/ac_pkg_swig.m4 b/config/ac_pkg_swig.m4
+index d12556e..9b608f2 100644
+--- a/config/ac_pkg_swig.m4
++++ b/config/ac_pkg_swig.m4
+@@ -78,9 +78,10 @@ AC_DEFUN([AC_PROG_SWIG],[
+ if test -z "$available_patch" ; then
+ [available_patch=0]
+ fi
+- if test $available_major -ne $required_major \
+- -o $available_minor -ne $required_minor \
+- -o $available_patch -lt $required_patch ; then
++ major_done=`test $available_major -gt $required_major`
++ minor_done=`test $available_minor -gt $required_minor`
++ if test !$major_done -a !$minor_done \
++ -a $available_patch -lt $required_patch ; then
+ AC_MSG_WARN([SWIG version >= $1 is required. You have $swig_version. You should look at http://www.swig.org])
+ SWIG=''
+ else
+diff --git a/src/Make.vars b/src/Make.vars
+index d70d6aa..93b2879 100644
+--- a/src/Make.vars
++++ b/src/Make.vars
+@@ -53,7 +53,7 @@ AM_LDFLAGS += -export-dynamic
+ endif
+
+ # set python runtimefiles to be installed in the same directory as pkg
+-pkglib_SCRIPTS = $(NOX_RUNTIMEFILES) $(NOX_PYBUILDFILES)
++pkgdata_SCRIPTS = $(NOX_RUNTIMEFILES) $(NOX_PYBUILDFILES)
+ BUILT_SOURCES = $(NOX_PYBUILDFILES)
+
+ # Runtime-files build and clean rules
+diff --git a/src/nox/coreapps/pyrt/deferredcallback.cc b/src/nox/coreapps/pyrt/deferredcallback.cc
+index 3a40fa7..111a586 100644
+--- a/src/nox/coreapps/pyrt/deferredcallback.cc
++++ b/src/nox/coreapps/pyrt/deferredcallback.cc
+@@ -69,7 +69,7 @@ DeferredCallback::get_instance(const Callback& c)
+ DeferredCallback* cb = new DeferredCallback(c);
+
+ // flag as used in *_wrap.cc....correct?
+- return SWIG_Python_NewPointerObj(cb, s, SWIG_POINTER_OWN | 0);
++ return SWIG_Python_NewPointerObj(m, cb, s, SWIG_POINTER_OWN | 0);
+ }
+
+ bool
+diff --git a/src/nox/coreapps/pyrt/pyglue.cc b/src/nox/coreapps/pyrt/pyglue.cc
+index 48b9716..317fd04 100644
+--- a/src/nox/coreapps/pyrt/pyglue.cc
++++ b/src/nox/coreapps/pyrt/pyglue.cc
+@@ -874,7 +874,7 @@ to_python(const Flow& flow)
+ if (!s) {
+ throw std::runtime_error("Could not find Flow SWIG type_info");
+ }
+- return SWIG_Python_NewPointerObj(f, s, SWIG_POINTER_OWN | 0);
++ return SWIG_Python_NewPointerObj(m, f, s, SWIG_POINTER_OWN | 0);
+
+ // PyObject* dict = PyDict_New();
+ // if (!dict) {
+diff --git a/src/nox/coreapps/pyrt/pyrt.cc b/src/nox/coreapps/pyrt/pyrt.cc
+index fbda461..8ec05d6 100644
+--- a/src/nox/coreapps/pyrt/pyrt.cc
++++ b/src/nox/coreapps/pyrt/pyrt.cc
+@@ -776,7 +776,7 @@ Python_event_manager::create_python_context(const Context* ctxt,
+ pretty_print_python_exception());
+ }
+
+- PyObject* pyctxt = SWIG_Python_NewPointerObj(p, s, 0);
++ PyObject* pyctxt = SWIG_Python_NewPointerObj(m, p, s, 0);
+ Py_INCREF(pyctxt); // XXX needed?
+
+ //Py_DECREF(m);
+diff --git a/src/nox/netapps/authenticator/auth.i b/src/nox/netapps/authenticator/auth.i
+index 1de1a17..bfa04e2 100644
+--- a/src/nox/netapps/authenticator/auth.i
++++ b/src/nox/netapps/authenticator/auth.i
+@@ -18,6 +18,8 @@
+
+ %module "nox.netapps.authenticator.pyauth"
+
++// Hack to get it to compile -BL
++%include "std_list.i"
+ %{
+ #include "core_events.hh"
+ #include "pyrt/pycontext.hh"
+diff --git a/src/nox/netapps/authenticator/flow_util.i b/src/nox/netapps/authenticator/flow_util.i
+index f67c3ef..2a314e2 100644
+--- a/src/nox/netapps/authenticator/flow_util.i
++++ b/src/nox/netapps/authenticator/flow_util.i
+@@ -32,6 +32,7 @@ using namespace vigil::applications;
+ %}
+
+ %include "common-defs.i"
++%include "std_list.i"
+
+ %import "netinet/netinet.i"
+ %import "pyrt/event.i"
+diff --git a/src/nox/netapps/routing/routing.i b/src/nox/netapps/routing/routing.i
+index 44ccb3d..f9221a2 100644
+--- a/src/nox/netapps/routing/routing.i
++++ b/src/nox/netapps/routing/routing.i
+@@ -17,6 +17,8 @@
+ */
+ %module "nox.netapps.routing.pyrouting"
+
++// Hack to get it to compile -BL
++%include "std_list.i"
+ %{
+ #include "pyrouting.hh"
+ #include "routing.hh"
+diff --git a/src/nox/netapps/switch_management/pyswitch_management.i b/src/nox/netapps/switch_management/pyswitch_management.i
+index 72bfed4..ad2c90d 100644
+--- a/src/nox/netapps/switch_management/pyswitch_management.i
++++ b/src/nox/netapps/switch_management/pyswitch_management.i
+@@ -18,6 +18,8 @@
+
+ %module "nox.netapps.pyswitch_management"
+
++// Hack to get it to compile -BL
++%include "std_list.i"
+ %{
+ #include "switch_management_proxy.hh"
+ #include "pyrt/pycontext.hh"
+diff --git a/src/nox/netapps/tests/tests.cc b/src/nox/netapps/tests/tests.cc
+index 20e900d..f027028 100644
+--- a/src/nox/netapps/tests/tests.cc
++++ b/src/nox/netapps/tests/tests.cc
+@@ -306,7 +306,7 @@ private:
+ throw runtime_error("Could not find PyContext SWIG type_info.");
+ }
+
+- PyObject* pyctxt = SWIG_Python_NewPointerObj(p, s, 0);
++ PyObject* pyctxt = SWIG_Python_NewPointerObj(m, p, s, 0);
+ assert(pyctxt);
+
+ Py_DECREF(m);
+diff --git a/src/nox/netapps/topology/pytopology.i b/src/nox/netapps/topology/pytopology.i
+index 94a9f4b..7a8cd94 100644
+--- a/src/nox/netapps/topology/pytopology.i
++++ b/src/nox/netapps/topology/pytopology.i
+@@ -18,6 +18,8 @@
+
+ %module "nox.netapps.topology"
+
++// Hack to get it to compile -BL
++%include "std_list.i"
+ %{
+ #include "pytopology.hh"
+ #include "pyrt/pycontext.hh"
+--
+1.7.5.4
+
diff --git a/util/nox-patches/README b/util/nox-patches/README
new file mode 100644
index 0000000..b74a668
--- /dev/null
+++ b/util/nox-patches/README
@@ -0,0 +1,2 @@
+0001: This patch adds the OpenFlow tutorial module source code to nox-destiny.
+0002: This patch hacks nox-destiny to compile on Ubuntu 12.04.
diff --git a/util/openflow-patches/README b/util/openflow-patches/README
new file mode 100644
index 0000000..36f19ad
--- /dev/null
+++ b/util/openflow-patches/README
@@ -0,0 +1,5 @@
+Patches for OpenFlow Reference Implementation
+
+controller.patch: patch controller to support up to 4096 switches (up from 16!)
+
+datapath.patch: patch to kernel datapath to compile with CONFIG_NET_NS=y
diff --git a/util/openflow-patches/controller.patch b/util/openflow-patches/controller.patch
new file mode 100644
index 0000000..c392fae
--- /dev/null
+++ b/util/openflow-patches/controller.patch
@@ -0,0 +1,15 @@
+diff --git a/controller/controller.c b/controller/controller.c
+index 41f2547..6eec590 100644
+--- a/controller/controller.c
++++ b/controller/controller.c
+@@ -58,8 +58,8 @@
+ #include "vlog.h"
+ #define THIS_MODULE VLM_controller
+
+-#define MAX_SWITCHES 16
+-#define MAX_LISTENERS 16
++#define MAX_SWITCHES 4096
++#define MAX_LISTENERS 4096
+
+ struct switch_ {
+ struct lswitch *lswitch;
diff --git a/util/openflow-patches/datapath.patch b/util/openflow-patches/datapath.patch
new file mode 100644
index 0000000..13c9df7
--- /dev/null
+++ b/util/openflow-patches/datapath.patch
@@ -0,0 +1,26 @@
+diff --git a/datapath/datapath.c b/datapath/datapath.c
+index 4a4d3a2..365aa25 100644
+--- a/datapath/datapath.c
++++ b/datapath/datapath.c
+@@ -47,6 +47,9 @@
+
+ #include "compat.h"
+
++#ifdef CONFIG_NET_NS
++#include <net/net_namespace.h>
++#endif
+
+ /* Strings to describe the manufacturer, hardware, and software. This data
+ * is queriable through the switch description stats message. */
+@@ -259,6 +262,10 @@ send_openflow_skb(const struct datapath *dp,
+ struct sk_buff *skb, const struct sender *sender)
+ {
+ return (sender
+- ? genlmsg_unicast(skb, sender->pid)
++#ifdef CONFIG_NET_NS
++ ? genlmsg_unicast(&init_net, skb, sender->pid)
++#else
++ ? genlmsg_unicast(skb, sender->pid)
++#endif
+ : genlmsg_multicast(skb, 0, dp_mc_group(dp), GFP_ATOMIC));
+ }
diff --git a/util/sch_htb-ofbuf/Makefile b/util/sch_htb-ofbuf/Makefile
new file mode 100644
index 0000000..c4d714f
--- /dev/null
+++ b/util/sch_htb-ofbuf/Makefile
@@ -0,0 +1,11 @@
+obj-m = sch_htb.o
+KVERSION = $(shell uname -r)
+all:
+ make -C /lib/modules/$(KVERSION)/build M=$(PWD) modules
+install:
+ test -e /lib/modules/$(KVERSION)/kernel/net/sched/sch_htb.ko.bak || mv /lib/modules/$(KVERSION)/kernel/net/sched/sch_htb.ko /lib/modules/$(KVERSION)/kernel/net/sched/sch_htb.ko.bak
+ cp sch_htb.ko /lib/modules/$(KVERSION)/kernel/net/sched/sch_htb.ko
+ rmmod sch_htb
+ modprobe sch_htb
+clean:
+ make -C /lib/modules/$(KVERSION)/build M=$(PWD) clean
diff --git a/util/sch_htb-ofbuf/README b/util/sch_htb-ofbuf/README
new file mode 100644
index 0000000..711ed77
--- /dev/null
+++ b/util/sch_htb-ofbuf/README
@@ -0,0 +1,10 @@
+Modified sch_htb implementation with ofbuf support.
+
+To compile, just type make. To use this module instead
+of regular sch_htb, do:
+
+0. make
+1. rmmod sch_htb
+2. insmod ./sch_htb.ko
+
+To revert, just rmmod sch_htb.
diff --git a/util/sch_htb-ofbuf/sch_htb.c b/util/sch_htb-ofbuf/sch_htb.c
new file mode 100644
index 0000000..baead1c
--- /dev/null
+++ b/util/sch_htb-ofbuf/sch_htb.c
@@ -0,0 +1,1644 @@
+#define OFBUF (1)
+/*
+ * net/sched/sch_htb.c Hierarchical token bucket, feed tree version
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Authors: Martin Devera, <devik@cdi.cz>
+ *
+ * Credits (in time order) for older HTB versions:
+ * Stef Coene <stef.coene@docum.org>
+ * HTB support at LARTC mailing list
+ * Ondrej Kraus, <krauso@barr.cz>
+ * found missing INIT_QDISC(htb)
+ * Vladimir Smelhaus, Aamer Akhter, Bert Hubert
+ * helped a lot to locate nasty class stall bug
+ * Andi Kleen, Jamal Hadi, Bert Hubert
+ * code review and helpful comments on shaping
+ * Tomasz Wrona, <tw@eter.tym.pl>
+ * created test case so that I was able to fix nasty bug
+ * Wilfried Weissmann
+ * spotted bug in dequeue code and helped with fix
+ * Jiri Fojtasek
+ * fixed requeue routine
+ * and many others. thanks.
+ */
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/errno.h>
+#include <linux/skbuff.h>
+#include <linux/list.h>
+#include <linux/compiler.h>
+#include <linux/rbtree.h>
+#include <linux/workqueue.h>
+#include <linux/slab.h>
+#include <net/netlink.h>
+#include <net/pkt_sched.h>
+
+/* HTB algorithm.
+ Author: devik@cdi.cz
+ ========================================================================
+ HTB is like TBF with multiple classes. It is also similar to CBQ because
+ it allows to assign priority to each class in hierarchy.
+ In fact it is another implementation of Floyd's formal sharing.
+
+ Levels:
+ Each class is assigned level. Leaf has ALWAYS level 0 and root
+ classes have level TC_HTB_MAXDEPTH-1. Interior nodes has level
+ one less than their parent.
+*/
+
+static int htb_hysteresis __read_mostly = 0; /* whether to use mode hysteresis for speedup */
+#define HTB_VER 0x30011 /* major must be matched with number suplied by TC as version */
+
+#if HTB_VER >> 16 != TC_HTB_PROTOVER
+#error "Mismatched sch_htb.c and pkt_sch.h"
+#endif
+
+/* Module parameter and sysfs export */
+module_param (htb_hysteresis, int, 0640);
+MODULE_PARM_DESC(htb_hysteresis, "Hysteresis mode, less CPU load, less accurate");
+
+/* used internaly to keep status of single class */
+enum htb_cmode {
+ HTB_CANT_SEND, /* class can't send and can't borrow */
+ HTB_MAY_BORROW, /* class can't send but may borrow */
+ HTB_CAN_SEND /* class can send */
+};
+
+/* interior & leaf nodes; props specific to leaves are marked L: */
+struct htb_class {
+ struct Qdisc_class_common common;
+ /* general class parameters */
+ struct gnet_stats_basic_packed bstats;
+ struct gnet_stats_queue qstats;
+ struct gnet_stats_rate_est rate_est;
+ struct tc_htb_xstats xstats; /* our special stats */
+ int refcnt; /* usage count of this class */
+
+ /* topology */
+ int level; /* our level (see above) */
+ unsigned int children;
+ struct htb_class *parent; /* parent class */
+
+ int prio; /* these two are used only by leaves... */
+ int quantum; /* but stored for parent-to-leaf return */
+
+ union {
+ struct htb_class_leaf {
+ struct Qdisc *q;
+ int deficit[TC_HTB_MAXDEPTH];
+ struct list_head drop_list;
+ } leaf;
+ struct htb_class_inner {
+ struct rb_root feed[TC_HTB_NUMPRIO]; /* feed trees */
+ struct rb_node *ptr[TC_HTB_NUMPRIO]; /* current class ptr */
+ /* When class changes from state 1->2 and disconnects from
+ * parent's feed then we lost ptr value and start from the
+ * first child again. Here we store classid of the
+ * last valid ptr (used when ptr is NULL).
+ */
+ u32 last_ptr_id[TC_HTB_NUMPRIO];
+ } inner;
+ } un;
+ struct rb_node node[TC_HTB_NUMPRIO]; /* node for self or feed tree */
+ struct rb_node pq_node; /* node for event queue */
+ psched_time_t pq_key;
+
+ int prio_activity; /* for which prios are we active */
+ enum htb_cmode cmode; /* current mode of the class */
+
+ /* class attached filters */
+ struct tcf_proto *filter_list;
+ int filter_cnt;
+
+ /* token bucket parameters */
+ struct qdisc_rate_table *rate; /* rate table of the class itself */
+ struct qdisc_rate_table *ceil; /* ceiling rate (limits borrows too) */
+ long buffer, cbuffer; /* token bucket depth/rate */
+ psched_tdiff_t mbuffer; /* max wait time */
+ long tokens, ctokens; /* current number of tokens */
+ psched_time_t t_c; /* checkpoint time */
+};
+
+struct htb_sched {
+ struct Qdisc_class_hash clhash;
+ struct list_head drops[TC_HTB_NUMPRIO];/* active leaves (for drops) */
+
+ /* self list - roots of self generating tree */
+ struct rb_root row[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
+ int row_mask[TC_HTB_MAXDEPTH];
+ struct rb_node *ptr[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
+ u32 last_ptr_id[TC_HTB_MAXDEPTH][TC_HTB_NUMPRIO];
+
+ /* self wait list - roots of wait PQs per row */
+ struct rb_root wait_pq[TC_HTB_MAXDEPTH];
+
+ /* time of nearest event per level (row) */
+ psched_time_t near_ev_cache[TC_HTB_MAXDEPTH];
+
+ int defcls; /* class where unclassified flows go to */
+
+ /* filters for qdisc itself */
+ struct tcf_proto *filter_list;
+
+ int rate2quantum; /* quant = rate / rate2quantum */
+ psched_time_t now; /* cached dequeue time */
+ struct qdisc_watchdog watchdog;
+
+ /* non shaped skbs; let them go directly thru */
+ struct sk_buff_head direct_queue;
+ int direct_qlen; /* max qlen of above */
+
+ long direct_pkts;
+
+#if OFBUF
+ /* overflow buffer */
+ struct sk_buff_head ofbuf;
+ int ofbuf_queued; /* # packets queued in above */
+#endif
+
+#define HTB_WARN_TOOMANYEVENTS 0x1
+ unsigned int warned; /* only one warning */
+ struct work_struct work;
+};
+
+/* find class in global hash table using given handle */
+static inline struct htb_class *htb_find(u32 handle, struct Qdisc *sch)
+{
+ struct htb_sched *q = qdisc_priv(sch);
+ struct Qdisc_class_common *clc;
+
+ clc = qdisc_class_find(&q->clhash, handle);
+ if (clc == NULL)
+ return NULL;
+ return container_of(clc, struct htb_class, common);
+}
+
+/**
+ * htb_classify - classify a packet into class
+ *
+ * It returns NULL if the packet should be dropped or -1 if the packet
+ * should be passed directly thru. In all other cases leaf class is returned.
+ * We allow direct class selection by classid in priority. The we examine
+ * filters in qdisc and in inner nodes (if higher filter points to the inner
+ * node). If we end up with classid MAJOR:0 we enqueue the skb into special
+ * internal fifo (direct). These packets then go directly thru. If we still
+ * have no valid leaf we try to use MAJOR:default leaf. It still unsuccessful
+ * then finish and return direct queue.
+ */
+#define HTB_DIRECT ((struct htb_class *)-1L)
+
+static struct htb_class *htb_classify(struct sk_buff *skb, struct Qdisc *sch,
+ int *qerr)
+{
+ struct htb_sched *q = qdisc_priv(sch);
+ struct htb_class *cl;
+ struct tcf_result res;
+ struct tcf_proto *tcf;
+ int result;
+
+ /* allow to select class by setting skb->priority to valid classid;
+ * note that nfmark can be used too by attaching filter fw with no
+ * rules in it
+ */
+ if (skb->priority == sch->handle)
+ return HTB_DIRECT; /* X:0 (direct flow) selected */
+ cl = htb_find(skb->priority, sch);
+ if (cl && cl->level == 0)
+ return cl;
+
+ *qerr = NET_XMIT_SUCCESS | __NET_XMIT_BYPASS;
+ tcf = q->filter_list;
+ while (tcf && (result = tc_classify(skb, tcf, &res)) >= 0) {
+#ifdef CONFIG_NET_CLS_ACT
+ switch (result) {
+ case TC_ACT_QUEUED:
+ case TC_ACT_STOLEN:
+ *qerr = NET_XMIT_SUCCESS | __NET_XMIT_STOLEN;
+ case TC_ACT_SHOT:
+ return NULL;
+ }
+#endif
+ cl = (void *)res.class;
+ if (!cl) {
+ if (res.classid == sch->handle)
+ return HTB_DIRECT; /* X:0 (direct flow) */
+ cl = htb_find(res.classid, sch);
+ if (!cl)
+ break; /* filter selected invalid classid */
+ }
+ if (!cl->level)
+ return cl; /* we hit leaf; return it */
+
+ /* we have got inner class; apply inner filter chain */
+ tcf = cl->filter_list;
+ }
+ /* classification failed; try to use default class */
+ cl = htb_find(TC_H_MAKE(TC_H_MAJ(sch->handle), q->defcls), sch);
+ if (!cl || cl->level)
+ return HTB_DIRECT; /* bad default .. this is safe bet */
+ return cl;
+}
+
+/**
+ * htb_add_to_id_tree - adds class to the round robin list
+ *
+ * Routine adds class to the list (actually tree) sorted by classid.
+ * Make sure that class is not already on such list for given prio.
+ */
+static void htb_add_to_id_tree(struct rb_root *root,
+ struct htb_class *cl, int prio)
+{
+ struct rb_node **p = &root->rb_node, *parent = NULL;
+
+ while (*p) {
+ struct htb_class *c;
+ parent = *p;
+ c = rb_entry(parent, struct htb_class, node[prio]);
+
+ if (cl->common.classid > c->common.classid)
+ p = &parent->rb_right;
+ else
+ p = &parent->rb_left;
+ }
+ rb_link_node(&cl->node[prio], parent, p);
+ rb_insert_color(&cl->node[prio], root);
+}
+
+/**
+ * htb_add_to_wait_tree - adds class to the event queue with delay
+ *
+ * The class is added to priority event queue to indicate that class will
+ * change its mode in cl->pq_key microseconds. Make sure that class is not
+ * already in the queue.
+ */
+static void htb_add_to_wait_tree(struct htb_sched *q,
+ struct htb_class *cl, long delay)
+{
+ struct rb_node **p = &q->wait_pq[cl->level].rb_node, *parent = NULL;
+
+ cl->pq_key = q->now + delay;
+ if (cl->pq_key == q->now)
+ cl->pq_key++;
+
+ /* update the nearest event cache */
+ if (q->near_ev_cache[cl->level] > cl->pq_key)
+ q->near_ev_cache[cl->level] = cl->pq_key;
+
+ while (*p) {
+ struct htb_class *c;
+ parent = *p;
+ c = rb_entry(parent, struct htb_class, pq_node);
+ if (cl->pq_key >= c->pq_key)
+ p = &parent->rb_right;
+ else
+ p = &parent->rb_left;
+ }
+ rb_link_node(&cl->pq_node, parent, p);
+ rb_insert_color(&cl->pq_node, &q->wait_pq[cl->level]);
+}
+
+/**
+ * htb_next_rb_node - finds next node in binary tree
+ *
+ * When we are past last key we return NULL.
+ * Average complexity is 2 steps per call.
+ */
+static inline void htb_next_rb_node(struct rb_node **n)
+{
+ *n = rb_next(*n);
+}
+
+/**
+ * htb_add_class_to_row - add class to its row
+ *
+ * The class is added to row at priorities marked in mask.
+ * It does nothing if mask == 0.
+ */
+static inline void htb_add_class_to_row(struct htb_sched *q,
+ struct htb_class *cl, int mask)
+{
+ q->row_mask[cl->level] |= mask;
+ while (mask) {
+ int prio = ffz(~mask);
+ mask &= ~(1 << prio);
+ htb_add_to_id_tree(q->row[cl->level] + prio, cl, prio);
+ }
+}
+
+/* If this triggers, it is a bug in this code, but it need not be fatal */
+static void htb_safe_rb_erase(struct rb_node *rb, struct rb_root *root)
+{
+ if (RB_EMPTY_NODE(rb)) {
+ WARN_ON(1);
+ } else {
+ rb_erase(rb, root);
+ RB_CLEAR_NODE(rb);
+ }
+}
+
+
+/**
+ * htb_remove_class_from_row - removes class from its row
+ *
+ * The class is removed from row at priorities marked in mask.
+ * It does nothing if mask == 0.
+ */
+static inline void htb_remove_class_from_row(struct htb_sched *q,
+ struct htb_class *cl, int mask)
+{
+ int m = 0;
+
+ while (mask) {
+ int prio = ffz(~mask);
+
+ mask &= ~(1 << prio);
+ if (q->ptr[cl->level][prio] == cl->node + prio)
+ htb_next_rb_node(q->ptr[cl->level] + prio);
+
+ htb_safe_rb_erase(cl->node + prio, q->row[cl->level] + prio);
+ if (!q->row[cl->level][prio].rb_node)
+ m |= 1 << prio;
+ }
+ q->row_mask[cl->level] &= ~m;
+}
+
+/**
+ * htb_activate_prios - creates active classe's feed chain
+ *
+ * The class is connected to ancestors and/or appropriate rows
+ * for priorities it is participating on. cl->cmode must be new
+ * (activated) mode. It does nothing if cl->prio_activity == 0.
+ */
+static void htb_activate_prios(struct htb_sched *q, struct htb_class *cl)
+{
+ struct htb_class *p = cl->parent;
+ long m, mask = cl->prio_activity;
+
+ while (cl->cmode == HTB_MAY_BORROW && p && mask) {
+ m = mask;
+ while (m) {
+ int prio = ffz(~m);
+ m &= ~(1 << prio);
+
+ if (p->un.inner.feed[prio].rb_node)
+ /* parent already has its feed in use so that
+ * reset bit in mask as parent is already ok
+ */
+ mask &= ~(1 << prio);
+
+ htb_add_to_id_tree(p->un.inner.feed + prio, cl, prio);
+ }
+ p->prio_activity |= mask;
+ cl = p;
+ p = cl->parent;
+
+ }
+ if (cl->cmode == HTB_CAN_SEND && mask)
+ htb_add_class_to_row(q, cl, mask);
+}
+
+/**
+ * htb_deactivate_prios - remove class from feed chain
+ *
+ * cl->cmode must represent old mode (before deactivation). It does
+ * nothing if cl->prio_activity == 0. Class is removed from all feed
+ * chains and rows.
+ */
+static void htb_deactivate_prios(struct htb_sched *q, struct htb_class *cl)
+{
+ struct htb_class *p = cl->parent;
+ long m, mask = cl->prio_activity;
+
+ while (cl->cmode == HTB_MAY_BORROW && p && mask) {
+ m = mask;
+ mask = 0;
+ while (m) {
+ int prio = ffz(~m);
+ m &= ~(1 << prio);
+
+ if (p->un.inner.ptr[prio] == cl->node + prio) {
+ /* we are removing child which is pointed to from
+ * parent feed - forget the pointer but remember
+ * classid
+ */
+ p->un.inner.last_ptr_id[prio] = cl->common.classid;
+ p->un.inner.ptr[prio] = NULL;
+ }
+
+ htb_safe_rb_erase(cl->node + prio, p->un.inner.feed + prio);
+
+ if (!p->un.inner.feed[prio].rb_node)
+ mask |= 1 << prio;
+ }
+
+ p->prio_activity &= ~mask;
+ cl = p;
+ p = cl->parent;
+
+ }
+ if (cl->cmode == HTB_CAN_SEND && mask)
+ htb_remove_class_from_row(q, cl, mask);
+}
+
+static inline long htb_lowater(const struct htb_class *cl)
+{
+ if (htb_hysteresis)
+ return cl->cmode != HTB_CANT_SEND ? -cl->cbuffer : 0;
+ else
+ return 0;
+}
+static inline long htb_hiwater(const struct htb_class *cl)
+{
+ if (htb_hysteresis)
+ return cl->cmode == HTB_CAN_SEND ? -cl->buffer : 0;
+ else
+ return 0;
+}
+
+
+/**
+ * htb_class_mode - computes and returns current class mode
+ *
+ * It computes cl's mode at time cl->t_c+diff and returns it. If mode
+ * is not HTB_CAN_SEND then cl->pq_key is updated to time difference
+ * from now to time when cl will change its state.
+ * Also it is worth to note that class mode doesn't change simply
+ * at cl->{c,}tokens == 0 but there can rather be hysteresis of
+ * 0 .. -cl->{c,}buffer range. It is meant to limit number of
+ * mode transitions per time unit. The speed gain is about 1/6.
+ */
+static inline enum htb_cmode
+htb_class_mode(struct htb_class *cl, long *diff)
+{
+ long toks;
+
+ if ((toks = (cl->ctokens + *diff)) < htb_lowater(cl)) {
+ *diff = -toks;
+ return HTB_CANT_SEND;
+ }
+
+ if ((toks = (cl->tokens + *diff)) >= htb_hiwater(cl))
+ return HTB_CAN_SEND;
+
+ *diff = -toks;
+ return HTB_MAY_BORROW;
+}
+
+/**
+ * htb_change_class_mode - changes classe's mode
+ *
+ * This should be the only way how to change classe's mode under normal
+ * cirsumstances. Routine will update feed lists linkage, change mode
+ * and add class to the wait event queue if appropriate. New mode should
+ * be different from old one and cl->pq_key has to be valid if changing
+ * to mode other than HTB_CAN_SEND (see htb_add_to_wait_tree).
+ */
+static void
+htb_change_class_mode(struct htb_sched *q, struct htb_class *cl, long *diff)
+{
+ enum htb_cmode new_mode = htb_class_mode(cl, diff);
+
+ if (new_mode == cl->cmode)
+ return;
+
+ if (cl->prio_activity) { /* not necessary: speed optimization */
+ if (cl->cmode != HTB_CANT_SEND)
+ htb_deactivate_prios(q, cl);
+ cl->cmode = new_mode;
+ if (new_mode != HTB_CANT_SEND)
+ htb_activate_prios(q, cl);
+ } else
+ cl->cmode = new_mode;
+}
+
+/**
+ * htb_activate - inserts leaf cl into appropriate active feeds
+ *
+ * Routine learns (new) priority of leaf and activates feed chain
+ * for the prio. It can be called on already active leaf safely.
+ * It also adds leaf into droplist.
+ */
+static inline void htb_activate(struct htb_sched *q, struct htb_class *cl)
+{
+ WARN_ON(cl->level || !cl->un.leaf.q || !cl->un.leaf.q->q.qlen);
+
+ if (!cl->prio_activity) {
+ cl->prio_activity = 1 << cl->prio;
+ htb_activate_prios(q, cl);
+ list_add_tail(&cl->un.leaf.drop_list,
+ q->drops + cl->prio);
+ }
+}
+
+/**
+ * htb_deactivate - remove leaf cl from active feeds
+ *
+ * Make sure that leaf is active. In the other words it can't be called
+ * with non-active leaf. It also removes class from the drop list.
+ */
+static inline void htb_deactivate(struct htb_sched *q, struct htb_class *cl)
+{
+ WARN_ON(!cl->prio_activity);
+
+ htb_deactivate_prios(q, cl);
+ cl->prio_activity = 0;
+ list_del_init(&cl->un.leaf.drop_list);
+}
+
+static int htb_enqueue(struct sk_buff *skb, struct Qdisc *sch)
+{
+ int uninitialized_var(ret);
+ struct htb_sched *q = qdisc_priv(sch);
+ struct htb_class *cl = htb_classify(skb, sch, &ret);
+
+#if OFBUF
+ if(cl != HTB_DIRECT && cl)
+ skb_get(skb);
+#endif
+
+ if (cl == HTB_DIRECT) {
+ /* enqueue to helper queue */
+ if (q->direct_queue.qlen < q->direct_qlen) {
+ __skb_queue_tail(&q->direct_queue, skb);
+ q->direct_pkts++;
+ } else {
+ kfree_skb(skb);
+ sch->qstats.drops++;
+ return NET_XMIT_DROP;
+ }
+#ifdef CONFIG_NET_CLS_ACT
+ } else if (!cl) {
+ if (ret & __NET_XMIT_BYPASS)
+ sch->qstats.drops++;
+ kfree_skb(skb);
+ return ret;
+#endif
+ } else if ((ret = qdisc_enqueue(skb, cl->un.leaf.q)) != NET_XMIT_SUCCESS) {
+ /* We shouldn't drop this, but enqueue it into ofbuf */
+ // TODO: is skb actually valid?
+ // Ans: looks like qdisc_enqueue will end up freeing the packet
+ // if enqueue failed. So we should incr refcnt before calling qdisc_enqueue...
+#if OFBUF
+ __skb_queue_tail(&q->ofbuf, skb);
+ q->ofbuf_queued++;
+#else
+ if (net_xmit_drop_count(ret)) {
+ sch->qstats.drops++;
+ cl->qstats.drops++;
+ }
+ return ret;
+#endif
+ } else {
+ bstats_update(&cl->bstats, skb);
+ htb_activate(q, cl);
+#if OFBUF
+ kfree_skb(skb);
+#endif
+ }
+
+ sch->q.qlen++;
+ return NET_XMIT_SUCCESS;
+}
+
+static inline void htb_accnt_tokens(struct htb_class *cl, int bytes, long diff)
+{
+ long toks = diff + cl->tokens;
+
+ if (toks > cl->buffer)
+ toks = cl->buffer;
+ toks -= (long) qdisc_l2t(cl->rate, bytes);
+ if (toks <= -cl->mbuffer)
+ toks = 1 - cl->mbuffer;
+
+ cl->tokens = toks;
+}
+
+static inline void htb_accnt_ctokens(struct htb_class *cl, int bytes, long diff)
+{
+ long toks = diff + cl->ctokens;
+
+ if (toks > cl->cbuffer)
+ toks = cl->cbuffer;
+ toks -= (long) qdisc_l2t(cl->ceil, bytes);
+ if (toks <= -cl->mbuffer)
+ toks = 1 - cl->mbuffer;
+
+ cl->ctokens = toks;
+}
+
+/**
+ * htb_charge_class - charges amount "bytes" to leaf and ancestors
+ *
+ * Routine assumes that packet "bytes" long was dequeued from leaf cl
+ * borrowing from "level". It accounts bytes to ceil leaky bucket for
+ * leaf and all ancestors and to rate bucket for ancestors at levels
+ * "level" and higher. It also handles possible change of mode resulting
+ * from the update. Note that mode can also increase here (MAY_BORROW to
+ * CAN_SEND) because we can use more precise clock that event queue here.
+ * In such case we remove class from event queue first.
+ */
+static void htb_charge_class(struct htb_sched *q, struct htb_class *cl,
+ int level, struct sk_buff *skb)
+{
+ int bytes = qdisc_pkt_len(skb);
+ enum htb_cmode old_mode;
+ long diff;
+
+ while (cl) {
+ diff = psched_tdiff_bounded(q->now, cl->t_c, cl->mbuffer);
+ if (cl->level >= level) {
+ if (cl->level == level)
+ cl->xstats.lends++;
+ htb_accnt_tokens(cl, bytes, diff);
+ } else {
+ cl->xstats.borrows++;
+ cl->tokens += diff; /* we moved t_c; update tokens */
+ }
+ htb_accnt_ctokens(cl, bytes, diff);
+ cl->t_c = q->now;
+
+ old_mode = cl->cmode;
+ diff = 0;
+ htb_change_class_mode(q, cl, &diff);
+ if (old_mode != cl->cmode) {
+ if (old_mode != HTB_CAN_SEND)
+ htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
+ if (cl->cmode != HTB_CAN_SEND)
+ htb_add_to_wait_tree(q, cl, diff);
+ }
+
+ /* update basic stats except for leaves which are already updated */
+ if (cl->level)
+ bstats_update(&cl->bstats, skb);
+
+ cl = cl->parent;
+ }
+}
+
+/**
+ * htb_do_events - make mode changes to classes at the level
+ *
+ * Scans event queue for pending events and applies them. Returns time of
+ * next pending event (0 for no event in pq, q->now for too many events).
+ * Note: Applied are events whose have cl->pq_key <= q->now.
+ */
+static psched_time_t htb_do_events(struct htb_sched *q, int level,
+ unsigned long start)
+{
+ /* don't run for longer than 2 jiffies; 2 is used instead of
+ * 1 to simplify things when jiffy is going to be incremented
+ * too soon
+ */
+ unsigned long stop_at = start + 2;
+ while (time_before(jiffies, stop_at)) {
+ struct htb_class *cl;
+ long diff;
+ struct rb_node *p = rb_first(&q->wait_pq[level]);
+
+ if (!p)
+ return 0;
+
+ cl = rb_entry(p, struct htb_class, pq_node);
+ if (cl->pq_key > q->now)
+ return cl->pq_key;
+
+ htb_safe_rb_erase(p, q->wait_pq + level);
+ diff = psched_tdiff_bounded(q->now, cl->t_c, cl->mbuffer);
+ htb_change_class_mode(q, cl, &diff);
+ if (cl->cmode != HTB_CAN_SEND)
+ htb_add_to_wait_tree(q, cl, diff);
+ }
+
+ /* too much load - let's continue after a break for scheduling */
+ if (!(q->warned & HTB_WARN_TOOMANYEVENTS)) {
+ pr_warning("htb: too many events!\n");
+ q->warned |= HTB_WARN_TOOMANYEVENTS;
+ }
+
+ return q->now;
+}
+
+/* Returns class->node+prio from id-tree where classe's id is >= id. NULL
+ * is no such one exists.
+ */
+static struct rb_node *htb_id_find_next_upper(int prio, struct rb_node *n,
+ u32 id)
+{
+ struct rb_node *r = NULL;
+ while (n) {
+ struct htb_class *cl =
+ rb_entry(n, struct htb_class, node[prio]);
+
+ if (id > cl->common.classid) {
+ n = n->rb_right;
+ } else if (id < cl->common.classid) {
+ r = n;
+ n = n->rb_left;
+ } else {
+ return n;
+ }
+ }
+ return r;
+}
+
+/**
+ * htb_lookup_leaf - returns next leaf class in DRR order
+ *
+ * Find leaf where current feed pointers points to.
+ */
+static struct htb_class *htb_lookup_leaf(struct rb_root *tree, int prio,
+ struct rb_node **pptr, u32 * pid)
+{
+ int i;
+ struct {
+ struct rb_node *root;
+ struct rb_node **pptr;
+ u32 *pid;
+ } stk[TC_HTB_MAXDEPTH], *sp = stk;
+
+ BUG_ON(!tree->rb_node);
+ sp->root = tree->rb_node;
+ sp->pptr = pptr;
+ sp->pid = pid;
+
+ for (i = 0; i < 65535; i++) {
+ if (!*sp->pptr && *sp->pid) {
+ /* ptr was invalidated but id is valid - try to recover
+ * the original or next ptr
+ */
+ *sp->pptr =
+ htb_id_find_next_upper(prio, sp->root, *sp->pid);
+ }
+ *sp->pid = 0; /* ptr is valid now so that remove this hint as it
+ * can become out of date quickly
+ */
+ if (!*sp->pptr) { /* we are at right end; rewind & go up */
+ *sp->pptr = sp->root;
+ while ((*sp->pptr)->rb_left)
+ *sp->pptr = (*sp->pptr)->rb_left;
+ if (sp > stk) {
+ sp--;
+ if (!*sp->pptr) {
+ WARN_ON(1);
+ return NULL;
+ }
+ htb_next_rb_node(sp->pptr);
+ }
+ } else {
+ struct htb_class *cl;
+ cl = rb_entry(*sp->pptr, struct htb_class, node[prio]);
+ if (!cl->level)
+ return cl;
+ (++sp)->root = cl->un.inner.feed[prio].rb_node;
+ sp->pptr = cl->un.inner.ptr + prio;
+ sp->pid = cl->un.inner.last_ptr_id + prio;
+ }
+ }
+ WARN_ON(1);
+ return NULL;
+}
+
+/* dequeues packet at given priority and level; call only if
+ * you are sure that there is active class at prio/level
+ */
+static struct sk_buff *htb_dequeue_tree(struct htb_sched *q, int prio,
+ int level)
+{
+ struct sk_buff *skb = NULL;
+ struct htb_class *cl, *start;
+ /* look initial class up in the row */
+ start = cl = htb_lookup_leaf(q->row[level] + prio, prio,
+ q->ptr[level] + prio,
+ q->last_ptr_id[level] + prio);
+
+ do {
+next:
+ if (unlikely(!cl))
+ return NULL;
+
+ /* class can be empty - it is unlikely but can be true if leaf
+ * qdisc drops packets in enqueue routine or if someone used
+ * graft operation on the leaf since last dequeue;
+ * simply deactivate and skip such class
+ */
+ if (unlikely(cl->un.leaf.q->q.qlen == 0)) {
+ struct htb_class *next;
+ htb_deactivate(q, cl);
+
+ /* row/level might become empty */
+ if ((q->row_mask[level] & (1 << prio)) == 0)
+ return NULL;
+
+ next = htb_lookup_leaf(q->row[level] + prio,
+ prio, q->ptr[level] + prio,
+ q->last_ptr_id[level] + prio);
+
+ if (cl == start) /* fix start if we just deleted it */
+ start = next;
+ cl = next;
+ goto next;
+ }
+
+ skb = cl->un.leaf.q->dequeue(cl->un.leaf.q);
+ if (likely(skb != NULL))
+ break;
+
+ qdisc_warn_nonwc("htb", cl->un.leaf.q);
+ htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
+ ptr[0]) + prio);
+ cl = htb_lookup_leaf(q->row[level] + prio, prio,
+ q->ptr[level] + prio,
+ q->last_ptr_id[level] + prio);
+
+ } while (cl != start);
+
+ if (likely(skb != NULL)) {
+ cl->un.leaf.deficit[level] -= qdisc_pkt_len(skb);
+ if (cl->un.leaf.deficit[level] < 0) {
+ cl->un.leaf.deficit[level] += cl->quantum;
+ htb_next_rb_node((level ? cl->parent->un.inner.ptr : q->
+ ptr[0]) + prio);
+ }
+ /* this used to be after charge_class but this constelation
+ * gives us slightly better performance
+ */
+ if (!cl->un.leaf.q->q.qlen)
+ htb_deactivate(q, cl);
+ htb_charge_class(q, cl, level, skb);
+ }
+ return skb;
+}
+
+static struct sk_buff *htb_dequeue(struct Qdisc *sch)
+{
+ struct sk_buff *skb;
+ struct htb_sched *q = qdisc_priv(sch);
+ int level;
+ psched_time_t next_event;
+ unsigned long start_at;
+ u32 r, i;
+ struct sk_buff *pkt;
+
+ /* try to dequeue direct packets as high prio (!) to minimize cpu work */
+ skb = __skb_dequeue(&q->direct_queue);
+ if (skb != NULL) {
+ok:
+ qdisc_bstats_update(sch, skb);
+ qdisc_unthrottled(sch);
+ sch->q.qlen--;
+#if OFBUF
+ if(q->ofbuf_queued > 0) {
+ i = 0;
+ r = net_random() % q->ofbuf_queued;
+ // enqueue the rth packet and drop the rest
+ while((pkt = __skb_dequeue(&q->ofbuf)) != NULL) {
+ if(i == r) {
+ // the chosen one
+ htb_enqueue(pkt, sch);
+ } else {
+ kfree_skb(pkt);
+ }
+ i++;
+ }
+ q->ofbuf_queued = 0;
+ }
+#endif
+ return skb;
+ }
+
+ if (!sch->q.qlen)
+ goto fin;
+ q->now = psched_get_time();
+ start_at = jiffies;
+
+ next_event = q->now + 5 * PSCHED_TICKS_PER_SEC;
+
+ for (level = 0; level < TC_HTB_MAXDEPTH; level++) {
+ /* common case optimization - skip event handler quickly */
+ int m;
+ psched_time_t event;
+
+ if (q->now >= q->near_ev_cache[level]) {
+ event = htb_do_events(q, level, start_at);
+ if (!event)
+ event = q->now + PSCHED_TICKS_PER_SEC;
+ q->near_ev_cache[level] = event;
+ } else
+ event = q->near_ev_cache[level];
+
+ if (next_event > event)
+ next_event = event;
+
+ m = ~q->row_mask[level];
+ while (m != (int)(-1)) {
+ int prio = ffz(m);
+
+ m |= 1 << prio;
+ skb = htb_dequeue_tree(q, prio, level);
+ if (likely(skb != NULL))
+ goto ok;
+ }
+ }
+ sch->qstats.overlimits++;
+ if (likely(next_event > q->now))
+ qdisc_watchdog_schedule(&q->watchdog, next_event);
+ else
+ schedule_work(&q->work);
+fin:
+ return skb;
+}
+
+/* try to drop from each class (by prio) until one succeed */
+static unsigned int htb_drop(struct Qdisc *sch)
+{
+ struct htb_sched *q = qdisc_priv(sch);
+ int prio;
+
+ for (prio = TC_HTB_NUMPRIO - 1; prio >= 0; prio--) {
+ struct list_head *p;
+ list_for_each(p, q->drops + prio) {
+ struct htb_class *cl = list_entry(p, struct htb_class,
+ un.leaf.drop_list);
+ unsigned int len;
+ if (cl->un.leaf.q->ops->drop &&
+ (len = cl->un.leaf.q->ops->drop(cl->un.leaf.q))) {
+ sch->q.qlen--;
+ if (!cl->un.leaf.q->q.qlen)
+ htb_deactivate(q, cl);
+ return len;
+ }
+ }
+ }
+ return 0;
+}
+
+/* reset all classes */
+/* always caled under BH & queue lock */
+static void htb_reset(struct Qdisc *sch)
+{
+ struct htb_sched *q = qdisc_priv(sch);
+ struct htb_class *cl;
+ struct hlist_node *n;
+ unsigned int i;
+
+ for (i = 0; i < q->clhash.hashsize; i++) {
+ hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
+ if (cl->level)
+ memset(&cl->un.inner, 0, sizeof(cl->un.inner));
+ else {
+ if (cl->un.leaf.q)
+ qdisc_reset(cl->un.leaf.q);
+ INIT_LIST_HEAD(&cl->un.leaf.drop_list);
+ }
+ cl->prio_activity = 0;
+ cl->cmode = HTB_CAN_SEND;
+
+ }
+ }
+ qdisc_watchdog_cancel(&q->watchdog);
+ __skb_queue_purge(&q->direct_queue);
+ sch->q.qlen = 0;
+#if OFBUF
+ __skb_queue_purge(&q->ofbuf);
+ q->ofbuf_queued = 0;
+#endif
+ memset(q->row, 0, sizeof(q->row));
+ memset(q->row_mask, 0, sizeof(q->row_mask));
+ memset(q->wait_pq, 0, sizeof(q->wait_pq));
+ memset(q->ptr, 0, sizeof(q->ptr));
+ for (i = 0; i < TC_HTB_NUMPRIO; i++)
+ INIT_LIST_HEAD(q->drops + i);
+}
+
+static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
+ [TCA_HTB_PARMS] = { .len = sizeof(struct tc_htb_opt) },
+ [TCA_HTB_INIT] = { .len = sizeof(struct tc_htb_glob) },
+ [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
+ [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
+};
+
+static void htb_work_func(struct work_struct *work)
+{
+ struct htb_sched *q = container_of(work, struct htb_sched, work);
+ struct Qdisc *sch = q->watchdog.qdisc;
+
+ __netif_schedule(qdisc_root(sch));
+}
+
+static int htb_init(struct Qdisc *sch, struct nlattr *opt)
+{
+ struct htb_sched *q = qdisc_priv(sch);
+ struct nlattr *tb[TCA_HTB_INIT + 1];
+ struct tc_htb_glob *gopt;
+ int err;
+ int i;
+
+ if (!opt)
+ return -EINVAL;
+
+ err = nla_parse_nested(tb, TCA_HTB_INIT, opt, htb_policy);
+ if (err < 0)
+ return err;
+
+ if (tb[TCA_HTB_INIT] == NULL) {
+ pr_err("HTB: hey probably you have bad tc tool ?\n");
+ return -EINVAL;
+ }
+ gopt = nla_data(tb[TCA_HTB_INIT]);
+ if (gopt->version != HTB_VER >> 16) {
+ pr_err("HTB: need tc/htb version %d (minor is %d), you have %d\n",
+ HTB_VER >> 16, HTB_VER & 0xffff, gopt->version);
+ return -EINVAL;
+ }
+
+ err = qdisc_class_hash_init(&q->clhash);
+ if (err < 0)
+ return err;
+ for (i = 0; i < TC_HTB_NUMPRIO; i++)
+ INIT_LIST_HEAD(q->drops + i);
+
+ qdisc_watchdog_init(&q->watchdog, sch);
+ INIT_WORK(&q->work, htb_work_func);
+ skb_queue_head_init(&q->direct_queue);
+
+#if OFBUF
+ skb_queue_head_init(&q->ofbuf);
+ q->ofbuf_queued = 0;
+#endif
+
+ q->direct_qlen = qdisc_dev(sch)->tx_queue_len;
+
+ if (q->direct_qlen < 2) /* some devices have zero tx_queue_len */
+ q->direct_qlen = 2;
+
+ if ((q->rate2quantum = gopt->rate2quantum) < 1)
+ q->rate2quantum = 1;
+ q->defcls = gopt->defcls;
+
+ return 0;
+}
+
+static int htb_dump(struct Qdisc *sch, struct sk_buff *skb)
+{
+ spinlock_t *root_lock = qdisc_root_sleeping_lock(sch);
+ struct htb_sched *q = qdisc_priv(sch);
+ struct nlattr *nest;
+ struct tc_htb_glob gopt;
+
+ spin_lock_bh(root_lock);
+
+ gopt.direct_pkts = q->direct_pkts;
+ gopt.version = HTB_VER;
+ gopt.rate2quantum = q->rate2quantum;
+ gopt.defcls = q->defcls;
+ gopt.debug = 0;
+
+ nest = nla_nest_start(skb, TCA_OPTIONS);
+ if (nest == NULL)
+ goto nla_put_failure;
+ NLA_PUT(skb, TCA_HTB_INIT, sizeof(gopt), &gopt);
+ nla_nest_end(skb, nest);
+
+ spin_unlock_bh(root_lock);
+ return skb->len;
+
+nla_put_failure:
+ spin_unlock_bh(root_lock);
+ nla_nest_cancel(skb, nest);
+ return -1;
+}
+
+static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
+ struct sk_buff *skb, struct tcmsg *tcm)
+{
+ struct htb_class *cl = (struct htb_class *)arg;
+ spinlock_t *root_lock = qdisc_root_sleeping_lock(sch);
+ struct nlattr *nest;
+ struct tc_htb_opt opt;
+
+ spin_lock_bh(root_lock);
+ tcm->tcm_parent = cl->parent ? cl->parent->common.classid : TC_H_ROOT;
+ tcm->tcm_handle = cl->common.classid;
+ if (!cl->level && cl->un.leaf.q)
+ tcm->tcm_info = cl->un.leaf.q->handle;
+
+ nest = nla_nest_start(skb, TCA_OPTIONS);
+ if (nest == NULL)
+ goto nla_put_failure;
+
+ memset(&opt, 0, sizeof(opt));
+
+ opt.rate = cl->rate->rate;
+ opt.buffer = cl->buffer;
+ opt.ceil = cl->ceil->rate;
+ opt.cbuffer = cl->cbuffer;
+ opt.quantum = cl->quantum;
+ opt.prio = cl->prio;
+ opt.level = cl->level;
+ NLA_PUT(skb, TCA_HTB_PARMS, sizeof(opt), &opt);
+
+ nla_nest_end(skb, nest);
+ spin_unlock_bh(root_lock);
+ return skb->len;
+
+nla_put_failure:
+ spin_unlock_bh(root_lock);
+ nla_nest_cancel(skb, nest);
+ return -1;
+}
+
+static int
+htb_dump_class_stats(struct Qdisc *sch, unsigned long arg, struct gnet_dump *d)
+{
+ struct htb_class *cl = (struct htb_class *)arg;
+
+ if (!cl->level && cl->un.leaf.q)
+ cl->qstats.qlen = cl->un.leaf.q->q.qlen;
+ cl->xstats.tokens = cl->tokens;
+ cl->xstats.ctokens = cl->ctokens;
+
+ if (gnet_stats_copy_basic(d, &cl->bstats) < 0 ||
+ gnet_stats_copy_rate_est(d, NULL, &cl->rate_est) < 0 ||
+ gnet_stats_copy_queue(d, &cl->qstats) < 0)
+ return -1;
+
+ return gnet_stats_copy_app(d, &cl->xstats, sizeof(cl->xstats));
+}
+
+static int htb_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
+ struct Qdisc **old)
+{
+ struct htb_class *cl = (struct htb_class *)arg;
+
+ if (cl->level)
+ return -EINVAL;
+ if (new == NULL &&
+ (new = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
+ cl->common.classid)) == NULL)
+ return -ENOBUFS;
+
+ sch_tree_lock(sch);
+ *old = cl->un.leaf.q;
+ cl->un.leaf.q = new;
+ if (*old != NULL) {
+ qdisc_tree_decrease_qlen(*old, (*old)->q.qlen);
+ qdisc_reset(*old);
+ }
+ sch_tree_unlock(sch);
+ return 0;
+}
+
+static struct Qdisc *htb_leaf(struct Qdisc *sch, unsigned long arg)
+{
+ struct htb_class *cl = (struct htb_class *)arg;
+ return !cl->level ? cl->un.leaf.q : NULL;
+}
+
+static void htb_qlen_notify(struct Qdisc *sch, unsigned long arg)
+{
+ struct htb_class *cl = (struct htb_class *)arg;
+
+ if (cl->un.leaf.q->q.qlen == 0)
+ htb_deactivate(qdisc_priv(sch), cl);
+}
+
+static unsigned long htb_get(struct Qdisc *sch, u32 classid)
+{
+ struct htb_class *cl = htb_find(classid, sch);
+ if (cl)
+ cl->refcnt++;
+ return (unsigned long)cl;
+}
+
+static inline int htb_parent_last_child(struct htb_class *cl)
+{
+ if (!cl->parent)
+ /* the root class */
+ return 0;
+ if (cl->parent->children > 1)
+ /* not the last child */
+ return 0;
+ return 1;
+}
+
+static void htb_parent_to_leaf(struct htb_sched *q, struct htb_class *cl,
+ struct Qdisc *new_q)
+{
+ struct htb_class *parent = cl->parent;
+
+ WARN_ON(cl->level || !cl->un.leaf.q || cl->prio_activity);
+
+ if (parent->cmode != HTB_CAN_SEND)
+ htb_safe_rb_erase(&parent->pq_node, q->wait_pq + parent->level);
+
+ parent->level = 0;
+ memset(&parent->un.inner, 0, sizeof(parent->un.inner));
+ INIT_LIST_HEAD(&parent->un.leaf.drop_list);
+ parent->un.leaf.q = new_q ? new_q : &noop_qdisc;
+ parent->tokens = parent->buffer;
+ parent->ctokens = parent->cbuffer;
+ parent->t_c = psched_get_time();
+ parent->cmode = HTB_CAN_SEND;
+}
+
+static void htb_destroy_class(struct Qdisc *sch, struct htb_class *cl)
+{
+ if (!cl->level) {
+ WARN_ON(!cl->un.leaf.q);
+ qdisc_destroy(cl->un.leaf.q);
+ }
+ gen_kill_estimator(&cl->bstats, &cl->rate_est);
+ qdisc_put_rtab(cl->rate);
+ qdisc_put_rtab(cl->ceil);
+
+ tcf_destroy_chain(&cl->filter_list);
+ kfree(cl);
+}
+
+static void htb_destroy(struct Qdisc *sch)
+{
+ struct htb_sched *q = qdisc_priv(sch);
+ struct hlist_node *n, *next;
+ struct htb_class *cl;
+ unsigned int i;
+
+ cancel_work_sync(&q->work);
+ qdisc_watchdog_cancel(&q->watchdog);
+ /* This line used to be after htb_destroy_class call below
+ * and surprisingly it worked in 2.4. But it must precede it
+ * because filter need its target class alive to be able to call
+ * unbind_filter on it (without Oops).
+ */
+ tcf_destroy_chain(&q->filter_list);
+
+ for (i = 0; i < q->clhash.hashsize; i++) {
+ hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode)
+ tcf_destroy_chain(&cl->filter_list);
+ }
+ for (i = 0; i < q->clhash.hashsize; i++) {
+ hlist_for_each_entry_safe(cl, n, next, &q->clhash.hash[i],
+ common.hnode)
+ htb_destroy_class(sch, cl);
+ }
+ qdisc_class_hash_destroy(&q->clhash);
+ __skb_queue_purge(&q->direct_queue);
+#if OFBUF
+ __skb_queue_purge(&q->ofbuf);
+ q->ofbuf_queued = 0;
+#endif
+}
+
+static int htb_delete(struct Qdisc *sch, unsigned long arg)
+{
+ struct htb_sched *q = qdisc_priv(sch);
+ struct htb_class *cl = (struct htb_class *)arg;
+ unsigned int qlen;
+ struct Qdisc *new_q = NULL;
+ int last_child = 0;
+
+ // TODO: why don't allow to delete subtree ? references ? does
+ // tc subsys quarantee us that in htb_destroy it holds no class
+ // refs so that we can remove children safely there ?
+ if (cl->children || cl->filter_cnt)
+ return -EBUSY;
+
+ if (!cl->level && htb_parent_last_child(cl)) {
+ new_q = qdisc_create_dflt(sch->dev_queue, &pfifo_qdisc_ops,
+ cl->parent->common.classid);
+ last_child = 1;
+ }
+
+ sch_tree_lock(sch);
+
+ if (!cl->level) {
+ qlen = cl->un.leaf.q->q.qlen;
+ qdisc_reset(cl->un.leaf.q);
+ qdisc_tree_decrease_qlen(cl->un.leaf.q, qlen);
+ }
+
+ /* delete from hash and active; remainder in destroy_class */
+ qdisc_class_hash_remove(&q->clhash, &cl->common);
+ if (cl->parent)
+ cl->parent->children--;
+
+ if (cl->prio_activity)
+ htb_deactivate(q, cl);
+
+ if (cl->cmode != HTB_CAN_SEND)
+ htb_safe_rb_erase(&cl->pq_node, q->wait_pq + cl->level);
+
+ if (last_child)
+ htb_parent_to_leaf(q, cl, new_q);
+
+ BUG_ON(--cl->refcnt == 0);
+ /*
+ * This shouldn't happen: we "hold" one cops->get() when called
+ * from tc_ctl_tclass; the destroy method is done from cops->put().
+ */
+
+ sch_tree_unlock(sch);
+ return 0;
+}
+
+static void htb_put(struct Qdisc *sch, unsigned long arg)
+{
+ struct htb_class *cl = (struct htb_class *)arg;
+
+ if (--cl->refcnt == 0)
+ htb_destroy_class(sch, cl);
+}
+
+static int htb_change_class(struct Qdisc *sch, u32 classid,
+ u32 parentid, struct nlattr **tca,
+ unsigned long *arg)
+{
+ int err = -EINVAL;
+ struct htb_sched *q = qdisc_priv(sch);
+ struct htb_class *cl = (struct htb_class *)*arg, *parent;
+ struct nlattr *opt = tca[TCA_OPTIONS];
+ struct qdisc_rate_table *rtab = NULL, *ctab = NULL;
+ struct nlattr *tb[__TCA_HTB_MAX];
+ struct tc_htb_opt *hopt;
+
+ /* extract all subattrs from opt attr */
+ if (!opt)
+ goto failure;
+
+ err = nla_parse_nested(tb, TCA_HTB_MAX, opt, htb_policy);
+ if (err < 0)
+ goto failure;
+
+ err = -EINVAL;
+ if (tb[TCA_HTB_PARMS] == NULL)
+ goto failure;
+
+ parent = parentid == TC_H_ROOT ? NULL : htb_find(parentid, sch);
+
+ hopt = nla_data(tb[TCA_HTB_PARMS]);
+
+ rtab = qdisc_get_rtab(&hopt->rate, tb[TCA_HTB_RTAB]);
+ ctab = qdisc_get_rtab(&hopt->ceil, tb[TCA_HTB_CTAB]);
+ if (!rtab || !ctab)
+ goto failure;
+
+ if (!cl) { /* new class */
+ struct Qdisc *new_q;
+ int prio;
+ struct {
+ struct nlattr nla;
+ struct gnet_estimator opt;
+ } est = {
+ .nla = {
+ .nla_len = nla_attr_size(sizeof(est.opt)),
+ .nla_type = TCA_RATE,
+ },
+ .opt = {
+ /* 4s interval, 16s averaging constant */
+ .interval = 2,
+ .ewma_log = 2,
+ },
+ };
+
+ /* check for valid classid */
+ if (!classid || TC_H_MAJ(classid ^ sch->handle) ||
+ htb_find(classid, sch))
+ goto failure;
+
+ /* check maximal depth */
+ if (parent && parent->parent && parent->parent->level < 2) {
+ pr_err("htb: tree is too deep\n");
+ goto failure;
+ }
+ err = -ENOBUFS;
+ cl = kzalloc(sizeof(*cl), GFP_KERNEL);
+ if (!cl)
+ goto failure;
+
+ err = gen_new_estimator(&cl->bstats, &cl->rate_est,
+ qdisc_root_sleeping_lock(sch),
+ tca[TCA_RATE] ? : &est.nla);
+ if (err) {
+ kfree(cl);
+ goto failure;
+ }
+
+ cl->refcnt = 1;
+ cl->children = 0;
+ INIT_LIST_HEAD(&cl->un.leaf.drop_list);
+ RB_CLEAR_NODE(&cl->pq_node);
+
+ for (prio = 0; prio < TC_HTB_NUMPRIO; prio++)
+ RB_CLEAR_NODE(&cl->node[prio]);
+
+ /* create leaf qdisc early because it uses kmalloc(GFP_KERNEL)
+ * so that can't be used inside of sch_tree_lock
+ * -- thanks to Karlis Peisenieks
+ */
+ new_q = qdisc_create_dflt(sch->dev_queue,
+ &pfifo_qdisc_ops, classid);
+ sch_tree_lock(sch);
+ if (parent && !parent->level) {
+ unsigned int qlen = parent->un.leaf.q->q.qlen;
+
+ /* turn parent into inner node */
+ qdisc_reset(parent->un.leaf.q);
+ qdisc_tree_decrease_qlen(parent->un.leaf.q, qlen);
+ qdisc_destroy(parent->un.leaf.q);
+ if (parent->prio_activity)
+ htb_deactivate(q, parent);
+
+ /* remove from evt list because of level change */
+ if (parent->cmode != HTB_CAN_SEND) {
+ htb_safe_rb_erase(&parent->pq_node, q->wait_pq);
+ parent->cmode = HTB_CAN_SEND;
+ }
+ parent->level = (parent->parent ? parent->parent->level
+ : TC_HTB_MAXDEPTH) - 1;
+ memset(&parent->un.inner, 0, sizeof(parent->un.inner));
+ }
+ /* leaf (we) needs elementary qdisc */
+ cl->un.leaf.q = new_q ? new_q : &noop_qdisc;
+
+ cl->common.classid = classid;
+ cl->parent = parent;
+
+ /* set class to be in HTB_CAN_SEND state */
+ cl->tokens = hopt->buffer;
+ cl->ctokens = hopt->cbuffer;
+ cl->mbuffer = 60 * PSCHED_TICKS_PER_SEC; /* 1min */
+ cl->t_c = psched_get_time();
+ cl->cmode = HTB_CAN_SEND;
+
+ /* attach to the hash list and parent's family */
+ qdisc_class_hash_insert(&q->clhash, &cl->common);
+ if (parent)
+ parent->children++;
+ } else {
+ if (tca[TCA_RATE]) {
+ err = gen_replace_estimator(&cl->bstats, &cl->rate_est,
+ qdisc_root_sleeping_lock(sch),
+ tca[TCA_RATE]);
+ if (err)
+ return err;
+ }
+ sch_tree_lock(sch);
+ }
+
+ /* it used to be a nasty bug here, we have to check that node
+ * is really leaf before changing cl->un.leaf !
+ */
+ if (!cl->level) {
+ cl->quantum = rtab->rate.rate / q->rate2quantum;
+ if (!hopt->quantum && cl->quantum < 1000) {
+ pr_warning(
+ "HTB: quantum of class %X is small. Consider r2q change.\n",
+ cl->common.classid);
+ cl->quantum = 1000;
+ }
+ if (!hopt->quantum && cl->quantum > 200000) {
+ pr_warning(
+ "HTB: quantum of class %X is big. Consider r2q change.\n",
+ cl->common.classid);
+ cl->quantum = 200000;
+ }
+ if (hopt->quantum)
+ cl->quantum = hopt->quantum;
+ if ((cl->prio = hopt->prio) >= TC_HTB_NUMPRIO)
+ cl->prio = TC_HTB_NUMPRIO - 1;
+ }
+
+ cl->buffer = hopt->buffer;
+ cl->cbuffer = hopt->cbuffer;
+ if (cl->rate)
+ qdisc_put_rtab(cl->rate);
+ cl->rate = rtab;
+ if (cl->ceil)
+ qdisc_put_rtab(cl->ceil);
+ cl->ceil = ctab;
+ sch_tree_unlock(sch);
+
+ qdisc_class_hash_grow(sch, &q->clhash);
+
+ *arg = (unsigned long)cl;
+ return 0;
+
+failure:
+ if (rtab)
+ qdisc_put_rtab(rtab);
+ if (ctab)
+ qdisc_put_rtab(ctab);
+ return err;
+}
+
+static struct tcf_proto **htb_find_tcf(struct Qdisc *sch, unsigned long arg)
+{
+ struct htb_sched *q = qdisc_priv(sch);
+ struct htb_class *cl = (struct htb_class *)arg;
+ struct tcf_proto **fl = cl ? &cl->filter_list : &q->filter_list;
+
+ return fl;
+}
+
+static unsigned long htb_bind_filter(struct Qdisc *sch, unsigned long parent,
+ u32 classid)
+{
+ struct htb_class *cl = htb_find(classid, sch);
+
+ /*if (cl && !cl->level) return 0;
+ * The line above used to be there to prevent attaching filters to
+ * leaves. But at least tc_index filter uses this just to get class
+ * for other reasons so that we have to allow for it.
+ * ----
+ * 19.6.2002 As Werner explained it is ok - bind filter is just
+ * another way to "lock" the class - unlike "get" this lock can
+ * be broken by class during destroy IIUC.
+ */
+ if (cl)
+ cl->filter_cnt++;
+ return (unsigned long)cl;
+}
+
+static void htb_unbind_filter(struct Qdisc *sch, unsigned long arg)
+{
+ struct htb_class *cl = (struct htb_class *)arg;
+
+ if (cl)
+ cl->filter_cnt--;
+}
+
+static void htb_walk(struct Qdisc *sch, struct qdisc_walker *arg)
+{
+ struct htb_sched *q = qdisc_priv(sch);
+ struct htb_class *cl;
+ struct hlist_node *n;
+ unsigned int i;
+
+ if (arg->stop)
+ return;
+
+ for (i = 0; i < q->clhash.hashsize; i++) {
+ hlist_for_each_entry(cl, n, &q->clhash.hash[i], common.hnode) {
+ if (arg->count < arg->skip) {
+ arg->count++;
+ continue;
+ }
+ if (arg->fn(sch, (unsigned long)cl, arg) < 0) {
+ arg->stop = 1;
+ return;
+ }
+ arg->count++;
+ }
+ }
+}
+
+static const struct Qdisc_class_ops htb_class_ops = {
+ .graft = htb_graft,
+ .leaf = htb_leaf,
+ .qlen_notify = htb_qlen_notify,
+ .get = htb_get,
+ .put = htb_put,
+ .change = htb_change_class,
+ .delete = htb_delete,
+ .walk = htb_walk,
+ .tcf_chain = htb_find_tcf,
+ .bind_tcf = htb_bind_filter,
+ .unbind_tcf = htb_unbind_filter,
+ .dump = htb_dump_class,
+ .dump_stats = htb_dump_class_stats,
+};
+
+static struct Qdisc_ops htb_qdisc_ops __read_mostly = {
+ .cl_ops = &htb_class_ops,
+ .id = "htb",
+ .priv_size = sizeof(struct htb_sched),
+ .enqueue = htb_enqueue,
+ .dequeue = htb_dequeue,
+ .peek = qdisc_peek_dequeued,
+ .drop = htb_drop,
+ .init = htb_init,
+ .reset = htb_reset,
+ .destroy = htb_destroy,
+ .dump = htb_dump,
+ .owner = THIS_MODULE,
+};
+
+static int __init htb_module_init(void)
+{
+ return register_qdisc(&htb_qdisc_ops);
+}
+static void __exit htb_module_exit(void)
+{
+ unregister_qdisc(&htb_qdisc_ops);
+}
+
+module_init(htb_module_init)
+module_exit(htb_module_exit)
+MODULE_LICENSE("GPL");
diff --git a/util/sysctl_addon b/util/sysctl_addon
new file mode 100644
index 0000000..e26eecc
--- /dev/null
+++ b/util/sysctl_addon
@@ -0,0 +1,17 @@
+# Mininet: Increase open file limit
+fs.file-max = 100000
+
+# Mininet: increase network buffer space
+net.core.wmem_max = 16777216
+net.core.rmem_max = 16777216
+net.ipv4.tcp_rmem = 10240 87380 16777216
+net.ipv4.tcp_rmem = 10240 87380 16777216
+net.core.netdev_max_backlog = 5000
+
+# Mininet: increase arp cache size
+net.ipv4.neigh.default.gc_thresh1 = 4096
+net.ipv4.neigh.default.gc_thresh2 = 8192
+net.ipv4.neigh.default.gc_thresh3 = 16384
+
+# Mininet: increase routing table size
+net.ipv4.route.max_size=32768
diff --git a/util/unpep8 b/util/unpep8
new file mode 100644
index 0000000..931b217
--- /dev/null
+++ b/util/unpep8
@@ -0,0 +1,198 @@
+#!/usr/bin/python
+
+"""
+Translate from PEP8 Python style to Mininet (i.e. Arista-like)
+Python style
+
+usage: unpep8 < old.py > new.py
+
+- Reinstates CapWords for methods and instance variables
+- Gets rid of triple single quotes
+- Eliminates triple quotes on single lines
+- Inserts extra spaces to improve readability
+- Fixes Doxygen (or doxypy) ugliness
+
+Does the following translations:
+
+ClassName.method_name(foo = bar) -> ClassName.methodName( foo=bar )
+
+Triple-single-quotes -> triple-double-quotes
+
+@param foo description -> foo: description
+@return description -> returns: description
+@author me -> author: me
+@todo(me) -> TODO(me)
+
+Bugs/Limitations:
+
+- Hack to restore strings is ugly
+- Multiline strings get mangled
+- Comments are mangled (which is arguably the "right thing" to do, except
+ that, for example, the left hand sides of the above would get translated!)
+- Doesn't eliminate unnecessary backslashes
+- Has no opinion on tab size
+- complicated indented docstrings get flattened
+- We don't (yet) have a filter to generate Doxygen/Doxypy
+- Currently leaves indents on blank comment lines
+- May lead to namespace collisions (e.g. some_thing and someThing)
+
+Bob Lantz, rlantz@cs.stanford.edu
+1/24/2010
+"""
+
+import re, sys
+
+def fixUnderscoreTriplet( match ):
+ "Translate a matched triplet of the form a_b to aB."
+ triplet = match.group()
+ return triplet[ :-2 ] + triplet[ -1 ].capitalize()
+
+def reinstateCapWords( text ):
+ underscoreTriplet = re.compile( r'[A-Za-z0-9]_[A-Za-z0-9]' )
+ return underscoreTriplet.sub( fixUnderscoreTriplet, text )
+
+def replaceTripleApostrophes( text ):
+ "Replace triple apostrophes with triple quotes."
+ return text.replace( "'''", '"""')
+
+def simplifyTripleQuotes( text ):
+ "Fix single-line doc strings."
+ r = re.compile( r'"""([^\"\n]+)"""' )
+ return r.sub( r'"\1"', text )
+
+def insertExtraSpaces( text ):
+ "Insert extra spaces inside of parentheses and brackets/curly braces."
+ lparen = re.compile( r'\((?![\s\)])' )
+ text = lparen.sub( r'( ', text )
+ rparen = re.compile( r'([^\s\(])(?=\))' )
+ text = rparen.sub( r'\1 ', text)
+ # brackets
+ lbrack = re.compile( r'\[(?![\s\]])' )
+ text = lbrack.sub( r'[ ', text )
+ rbrack = re.compile( r'([^\s\[])(?=\])' )
+ text = rbrack.sub( r'\1 ', text)
+ # curly braces
+ lcurly = re.compile( r'\{(?![\s\}])' )
+ text = lcurly.sub( r'{ ', text )
+ rcurly = re.compile( r'([^\s\{])(?=\})' )
+ text = rcurly.sub( r'\1 ', text)
+ return text
+
+def fixDoxygen( text ):
+ """Translate @param foo to foo:, @return bar to returns: bar, and
+ @author me to author: me"""
+ param = re.compile( r'@param (\w+)' )
+ text = param.sub( r'\1:', text )
+ returns = re.compile( r'@return' )
+ text = returns.sub( r'returns:', text )
+ author = re.compile( r'@author' )
+ text = author.sub( r'author:', text)
+ # @todo -> TODO
+ text = text.replace( '@todo', 'TODO' )
+ return text
+
+def removeCommentFirstBlankLine( text ):
+ "Remove annoying blank lines after first line in comments."
+ line = re.compile( r'("""[^\n]*\n)\s*\n', re.MULTILINE )
+ return line.sub( r'\1', text )
+
+def fixArgs( match, kwarg = re.compile( r'(\w+) = ' ) ):
+ "Replace foo = bar with foo=bar."
+ return kwarg.sub( r'\1=', match.group() )
+
+def fixKeywords( text ):
+ "Change keyword argumentsfrom foo = bar to foo=bar."
+ args = re.compile( r'\(([^\)]+)\)', re.MULTILINE )
+ return args.sub( fixArgs, text )
+
+# Unfortunately, Python doesn't natively support balanced or recursive
+# regular expressions. We could use PyParsing, but that opens another can
+# of worms. For now, we just have a cheap hack to restore strings,
+# so we don't end up accidentally mangling things like messages, search strings,
+# and regular expressions.
+
+def lineIter( text ):
+ "Simple iterator over lines in text."
+ for line in text.splitlines(): yield line
+
+def stringIter( strList ):
+ "Yield strings in strList."
+ for s in strList: yield s
+
+def restoreRegex( regex, old, new ):
+ "Find regexes in old and restore them into new."
+ oldStrs = regex.findall( old )
+ # Sanity check - count should be the same!
+ newStrs = regex.findall( new )
+ assert len( oldStrs ) == len( newStrs )
+ # Replace newStrs with oldStrs
+ siter = stringIter( oldStrs )
+ reps = lambda dummy: siter.next()
+ return regex.sub( reps, new )
+
+# This is a cheap hack, and it may not work 100%, since
+# it doesn't handle multiline strings.
+# However, it should be mostly harmless...
+
+def restoreStrings( oldText, newText ):
+ "Restore strings from oldText into newText, returning result."
+ oldLines, newLines = lineIter( oldText ), lineIter( newText )
+ quoteStrings = re.compile( r'("[^"]*")' )
+ tickStrings = re.compile( r"('[^']*')" )
+ result = ''
+ # It would be nice if we could blast the whole file, but for
+ # now it seems to work line-by-line
+ for newLine in newLines:
+ oldLine = oldLines.next()
+ newLine = restoreRegex( quoteStrings, oldLine, newLine )
+ newLine = restoreRegex( tickStrings, oldLine, newLine )
+ result += newLine + '\n'
+ return result
+
+# This might be slightly controversial, since it uses
+# three spaces to line up multiline comments. However,
+# I much prefer it. Limitations: if you have deeper
+# indents in comments, they will be eliminated. ;-(
+
+def fixComment( match,
+ indentExp=re.compile( r'\n([ ]*)(?=[^/s])', re.MULTILINE ),
+ trailingQuotes=re.compile( r'\s+"""' ) ):
+ "Re-indent comment, and join trailing quotes."
+ originalIndent = match.group( 1 )
+ comment = match.group( 2 )
+ indent = '\n' + originalIndent
+ # Exception: leave unindented things unindented!
+ if len( originalIndent ) is not 0: indent += ' '
+ comment = indentExp.sub( indent, comment )
+ return originalIndent + trailingQuotes.sub( '"""', comment )
+
+def fixCommentIndents( text ):
+ "Fix multiline comment indentation."
+ comments = re.compile( r'^([ ]*)("""[^"]*""")$', re.MULTILINE )
+ return comments.sub( fixComment, text )
+
+def removeBogusLinefeeds( text ):
+ "Remove extra linefeeds at the end of single-line comments."
+ bogusLfs = re.compile( r'"([^"\n]*)\n"', re.MULTILINE )
+ return bogusLfs.sub( '"\1"', text)
+
+def convertFromPep8( program ):
+ oldProgram = program
+ # Program text transforms
+ program = reinstateCapWords( program )
+ program = fixKeywords( program )
+ program = insertExtraSpaces( program )
+ # Undo string damage
+ program = restoreStrings( oldProgram, program )
+ # Docstring transforms
+ program = replaceTripleApostrophes( program )
+ program = simplifyTripleQuotes( program )
+ program = fixDoxygen( program )
+ program = fixCommentIndents( program )
+ program = removeBogusLinefeeds( program )
+ # Destructive transforms (these can delete lines)
+ program = removeCommentFirstBlankLine( program )
+ return program
+
+if __name__ == '__main__':
+ print convertFromPep8( sys.stdin.read() )
\ No newline at end of file
diff --git a/util/versioncheck.py b/util/versioncheck.py
new file mode 100644
index 0000000..d9e5483
--- /dev/null
+++ b/util/versioncheck.py
@@ -0,0 +1,24 @@
+#!/usr/bin/python
+
+from subprocess import check_output as co
+from sys import exit
+
+# Actually run bin/mn rather than importing via python path
+version = 'Mininet ' + co( 'PYTHONPATH=. bin/mn --version', shell=True )
+version = version.strip()
+
+# Find all Mininet path references
+lines = co( "grep -or 'Mininet \w\.\w\.\w\w*' *", shell=True )
+
+error = False
+
+for line in lines.split( '\n' ):
+ if line and 'Binary' not in line:
+ fname, fversion = line.split( ':' )
+ if version != fversion:
+ print "%s: incorrect version '%s' (should be '%s')" % (
+ fname, fversion, version )
+ error = True
+
+if error:
+ exit( 1 )
diff --git a/util/vm/.bash_profile b/util/vm/.bash_profile
new file mode 100644
index 0000000..9935934
--- /dev/null
+++ b/util/vm/.bash_profile
@@ -0,0 +1,24 @@
+SSH_ENV="$HOME/.ssh/environment"
+
+function start_agent {
+ echo "Initialising new SSH agent..."
+ /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
+ echo succeeded
+ chmod 600 "${SSH_ENV}"
+ . "${SSH_ENV}" > /dev/null
+ /usr/bin/ssh-add;
+}
+
+# Source SSH settings, if applicable
+
+if [ -f "${SSH_ENV}" ]; then
+ . "${SSH_ENV}" > /dev/null
+ #ps ${SSH_AGENT_PID} doesn't work under cywgin
+ ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
+ start_agent;
+ }
+else
+ start_agent;
+fi
+
+source ~/.bashrc
diff --git a/util/vm/install-mininet-vm.sh b/util/vm/install-mininet-vm.sh
new file mode 100644
index 0000000..382945e
--- /dev/null
+++ b/util/vm/install-mininet-vm.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+# This script is intended to install Mininet into
+# a brand-new Ubuntu virtual machine,
+# to create a fully usable "tutorial" VM.
+set -e
+echo `whoami` ALL=NOPASSWD: ALL | sudo tee -a /etc/sudoers
+sudo sed -i -e 's/Default/#Default/' /etc/sudoers
+sudo sed -i -e 's/ubuntu/mininet-vm/' /etc/hostname
+sudo sed -i -e 's/ubuntu/mininet-vm/g' /etc/hosts
+sudo hostname `cat /etc/hostname`
+sudo sed -i -e 's/quiet splash/text/' /etc/default/grub
+sudo update-grub
+sudo sed -i -e 's/us.archive.ubuntu.com/mirrors.kernel.org/' \
+ /etc/apt/sources.list
+sudo apt-get update
+# Clean up vmware easy install junk if present
+if [ -e /etc/issue.backup ]; then
+ sudo mv /etc/issue.backup /etc/issue
+fi
+if [ -e /etc/rc.local.backup ]; then
+ sudo mv /etc/rc.local.backup /etc/rc.local
+fi
+# Install Mininet
+sudo apt-get -y install git-core openssh-server
+git clone git://github.com/mininet/mininet
+cd mininet
+cd
+time mininet/util/install.sh
+# Ignoring this since NOX classic is deprecated
+#if ! grep NOX_CORE_DIR .bashrc; then
+# echo "export NOX_CORE_DIR=~/noxcore/build/src/" >> .bashrc
+#fi
+echo <<EOF
+You may need to reboot and then:
+sudo dpkg-reconfigure openvswitch-datapath-dkms
+sudo service openvswitch-switch start
+EOF
+
+