# # Common functions used by pktgen scripts # - Depending on bash 3 (or higher) syntax # # Author: Jesper Dangaaard Brouer # License: GPL ## -- General shell logging cmds -- proc err { local exitcode=$1 shift echo "ERROR: $ifsjoin(ARGV)" >&2 exit $exitcode } proc warn { echo "WARN : $ifsjoin(ARGV)" >&2 } proc info { if [[ -n "$VERBOSE" ]] { echo "INFO : $ifsjoin(ARGV)" >&2 } } ## -- Pktgen proc config commands -- ## export PROC_DIR=/proc/net/pktgen # # Three different shell functions for configuring the different # components of pktgen: # pg_ctrl(), pg_thread() and pg_set(). # # These functions correspond to pktgens different components. # * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl) # * pg_thread() control the kernel threads and binding to devices # * pg_set() control setup of individual devices proc pg_ctrl { local proc_file="pgctrl" proc_cmd $(proc_file) @ARGV } proc pg_thread { local thread=$1 local proc_file="kpktgend_$(thread)" shift proc_cmd $(proc_file) @ARGV } proc pg_set { local dev=$1 local proc_file="$dev" shift proc_cmd $(proc_file) @ARGV } # More generic replacement for pgset(), that does not depend on global # variable for proc file. proc proc_cmd { local result local proc_file=$1 # after shift, the remaining args are contained in $@ shift local proc_ctrl=$(PROC_DIR)/$proc_file if [[ ! -e "$proc_ctrl" ]] { err 3 "proc file:$proc_ctrl does not exists (dev added to thread?)" } else { if [[ ! -w "$proc_ctrl" ]] { err 4 "proc file:$proc_ctrl not writable, not root?!" } } if [[ "$DEBUG" == "yes" ]] { echo "cmd: $ifsjoin(ARGV) > $proc_ctrl" } # Quoting of "$@" is important for space expansion echo @ARGV > "$proc_ctrl" local status=$Status setglobal result = $[grep "Result: OK:" $proc_ctrl] # Due to pgctrl, cannot use exit code $? from grep if [[ "$result" == "" ]] { grep "Result:" $proc_ctrl >&2 } if sh-expr ' $status != 0 ' { err 5 "Write error($status) occurred cmd: \"$ifsjoin(ARGV) > $proc_ctrl\"" } } # Old obsolete "pgset" function, with slightly improved err handling proc pgset { local result if [[ "$DEBUG" == "yes" ]] { echo "cmd: $1 > $PGDEV" } echo $1 > $PGDEV local status=$Status setglobal result = $[cat $PGDEV | fgrep "Result: OK:] if [[ "$result" == "" ]] { cat $PGDEV | fgrep Result: } if sh-expr ' $status != 0 ' { err 5 "Write error($status) occurred cmd: \"$1 > $PGDEV\"" } } ## -- General shell tricks -- proc root_check_run_with_sudo { # Trick so, program can be run as normal user, will just use "sudo" # call as root_check_run_as_sudo "$@" if test $EUID -ne 0 { if test -x $0 { # Directly executable use sudo info "Not root, running with sudo" sudo $0 @ARGV exit $? } err 4 "cannot perform sudo run of $0" } }