4 # Copyright (C) 2009 UniPi
7 # This script is the backend to be used with
9 # It allows to configure dummynet pipes and queues.
11 # - read the user's input from the input pipe
12 # - validate the input
14 # - put results on the output vsys pipe
16 # This script expect to read from the input vsys
17 # pipe a line formatted as follow:
18 # ${PORT} ${TIMEOUT} <dummynet parameters>
19 # the timeout value is expressed as:
20 # week, day, month or anything else accepted by the date command
25 LOG_FILE=/tmp/netconfig.log
32 # set to 0 to disable debug messages
35 debug() { # $1 message to be displayed
36 [ x"${DEBUG}" != x"0" ] && echo $1 >>{LOG_FILE};
39 abort() { # $1 message to be displayed
44 user_error() { # $1 message to be displayed
45 echo "1 User error: $1"
49 filter() { # $* variables to be filtered
50 # allowed chars are: numbers, upcase and lowecase
51 # chars, and the following symbols: . _ - /
52 echo "$*" | ${SED} -r 's/[^0-9a-zA-Z. _\/\-]*//g'
55 # Add ipfw pipe and rules
56 # We use the PORT number to configure the
57 # pipe, and add rules for that port.
58 # The default directory is the slicename root
59 add_rules() { # $1 timeout value $2 delete
62 debug "Add a new rule, check for deletion flag";
63 if [ ${2} -eq 1 ]; then
64 #echo "Rules and pipes deleted";
68 debug "Add a new rule"
69 # schedule the rule deletion
70 EXPIRE=`date --date="${TIMEOUT}" +%s`
71 [ x"${EXPIRE}" = x"" ] && abort "Date format $1 not valid"
73 # move in the slice root dir
74 cd /vservers/${SLICE}/root
75 #echo ${CONFIG_STRING} | ${SED} -e "s/ profile \(.[^ ]\)/ profile \/vservers\/${SLICE}\/\1/g"
77 # check syntax, if ok execute
79 local IPFW_CHECK="${IPFW} -n "
83 ${IPFW_CHECK} add ${RULE_N} pipe ${PIPE_N} ip from ${ME} to any src-port ${PORT} // ${EXPIRE} ${SLICE}
86 ${IPFW_CHECK} add ${RULE_N} pipe ${PIPE_N} ip from any to ${ME} dst-port ${PORT}
90 ${IPFW_CHECK} pipe ${PIPE_N} config ${CONFIG_STRING}
92 if [ ! $ERROR -eq 0 ]; then
93 echo "Some errors occurred not executing"
94 user_error "ipfw syntax error"
98 ${IPFW} add ${RULE_N} pipe ${PIPE_N} ip from ${ME} to any src-port ${PORT} // ${EXPIRE} ${SLICE}
99 ${IPFW} add ${RULE_N} pipe ${PIPE_N} ip from any to ${ME} dst-port ${PORT}
102 ${IPFW} pipe ${PIPE_N} config ${CONFIG_STRING}
105 # Delete a given link
108 ipfw delete ${RULE_N}
109 ipfw pipe delete ${RULE_N}
112 # The rule we want to configure already exist.
113 # Check for slice owner matching.
118 RULE=`ipfw list ${PORT} 2>&1 | cut -d ' ' -f 12`;
119 if [ "${RULE}" = "${SLICE}" ] ; then # replace the link configuration
120 debug "The rule already exist, the owner match, delete old rule"
123 add_rules ${TIMEOUT} ${DELETE}
125 user_error "the rule already exist, ant you are not the slice owner, try later"
129 # process a single line of input
130 # this line has the following format:
133 # port timeout configuration_string
136 local TMP; # temporary var
138 debug "Received from the input pipe: $1"
140 # allow netconfig ipfw show
141 # allow netconfig pipe show
143 CMD=`echo $1 | cut -d\ -f 1`
144 if [ x${CMD} == x"ipfw" ]; then
147 else if [ x${CMD} == x"pipe" ]; then
153 ARGS=`echo $1 | wc -w`
154 if [ $ARGS -le 2 ]; then
155 abort "One or more input parameter is missing"
159 TMP=`echo $1 | cut -d\ -f 1`
161 TMP=`echo $1 | cut -d\ -f 2`
162 TIMEOUT=`filter $TMP`
163 TMP=`echo $1 | cut -d\ -f 3`
165 TMP=`echo $1 | cut -d\ -f 4-`
166 CONFIG_STRING=`filter $TMP`
169 debug "DELETE: $DELETE"
170 debug "TIMEOUT: $TIMEOUT"
171 debug "configuration string: $CONFIG_STRING"
173 # find the ip address
174 ME=`/sbin/ip -o addr show | grep -v "1:\ lo" | grep "inet " | cut -d " " -f7 | cut -d "/" -f1 | head -n1`
177 [ ${PORT} -le 1024 ] && user_error "it is not allowed to modify the port range [0-1024]"
179 # start to configure pipes and rules
183 # check if the link is already configured
184 ipfw list ${PORT} 2>&1
186 if [ x"$?" != x"0" ]; then # new rule, add and set owner/timeout
187 add_rules ${TIMEOUT} ${DELETE}
188 else # the rule already exist, check owner
196 debug "Debug activated"
202 # read -a read arguments in array
203 # XXX skip lines starting with #
204 requests[$i]=$request;
211 for i in `/usr/bin/seq 0 $((${#requests[*]} - 1))`
213 process "${requests[$i]}"