Import source code for dummynet innode emulation.
[ipfw.git] / slice / netconfig
diff --git a/slice/netconfig b/slice/netconfig
new file mode 100755 (executable)
index 0000000..50fe781
--- /dev/null
@@ -0,0 +1,124 @@
+#!/bin/sh
+#
+# Marta Carbone
+# Copyright (C) 2009 Universita` di Pisa
+# $Id$
+#
+# This script is the frontend to be used with
+# the vsys system.
+# It allows to configure dummynet pipes and queues.
+# In detail it:
+# - get user input
+# - send command to the vsys input pipe
+# - wait a bit
+# - get the reply from the output vsys pipe
+# - show the output to the user
+# 
+# A simple usage is:
+# ./netconfig -p 9898 <dummynet configuration parameters>
+# mandatory fields are the port to be configured
+# and dummynet configuration parameters
+
+PIPE_IN=/vsys/ipfw-be.in
+PIPE_OUT=/vsys/ipfw-be.out
+LOGFILE=/tmp/netconfig.log
+
+SED=/bin/sed           # used to extract the profile name
+DEBUG=0                        # 0 disable debug messages
+prog=$0                        # this program
+
+# Print the first argument and exit
+abort()
+{
+       echo "$1"; exit 1
+}
+
+# if $DEBUG is enabled, print a debug message
+# $1 the message to print
+debug()
+{
+       [ "${DEBUG}" != "0" ] && echo "$1" >> ${LOGFILE}
+}
+
+# print the usage and exit
+help()
+{
+       cat << EOF
+Usage: $prog [-n node_ip][-s slice] [-i key] -d dummynetbox_ip -p port [-U] [-t timeout[STRING]] ipfw_configuration_parameters \n
+  -h show help \n
+  -p specify the port to configure \n
+  -t specify the timeout. STRING can be "1minute" or "4hours" (default 1 hour) \n
+
+   ipfw configuration parameters (mandatory) \n
+          extra-delay <filename> bw <value> delay <value> proto... plr... \n
+EOF
+       exit 0
+}
+
+# parse input line and fill missing fields
+parse_input()
+{
+       # counters used to check if at least
+       # two ipfw configuration parameters are present
+       local OPT_ARGS=2        # optional (timeout)
+       local MAND_ARGS=2       # mandatory (port)
+
+       [ -z "$1" ] && help;
+
+       while getopts ":hp:t:" opt; do
+               case $opt in
+               h | \?) help;;
+               p) PORT=$OPTARG;;
+               t) TIMEOUT=$OPTARG;;
+               esac
+       done
+
+       # check for mandatory arguments
+       [ -z ${PORT} ] && abort "a port value is mandatory";
+
+       # the default value for the timeout is 1H
+       if [ -z ${TIMEOUT} ]; then
+               TIMEOUT="1hour"
+               OPT_ARGS=$((${OPT_ARGS}-2))
+       fi
+
+       # compute residue argument, we need at least 2 
+       # mandatory arguments (for ipfw), exit on error
+       #debug "Passed args $# Mandatory ${MAND_ARGS} Optional ${OPT_ARGS} Extra $(($#-${MAND_ARGS}-${OPT_ARGS}))"
+       if [ $(($#-${MAND_ARGS}-${OPT_ARGS})) -lt 2 ]; then
+               help
+       fi
+}
+
+# main starts here
+
+       # allow ipfw show and ipfw pipe show
+       if [ x$1 = x"ipfw" ]; then
+               echo "received" $1
+               echo "ipfw" >> ${PIPE_IN}
+               cat ${PIPE_OUT}
+               exit 0;
+       else if [ x$1 = x"pipe" ]; then
+               echo "pipe" >> ${PIPE_IN}
+               cat ${PIPE_OUT}
+               exit 0;
+       fi
+       fi
+
+       # parse the input
+       parse_input $*
+       shift $((${OPTIND}-1));
+
+       # print debug
+       debug "PORT ${PORT}"
+       debug "TIMEOUT ${TIMEOUT}"
+       debug "PIPE_CONFIGURATION $*"
+
+       # format CMD as expected by the backend script
+       CMD="${PORT} ${TIMEOUT} $*";
+
+       # send the command
+       debug "Sending: ${CMD}"
+       echo ${CMD} >> ${PIPE_IN}
+
+       cat ${PIPE_OUT}