# # 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 { var 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 { var proc_file = '"pgctrl'" proc_cmd $(proc_file) @Argv } proc pg_thread { var thread = $1 var proc_file = ""kpktgend_$(thread)"" shift proc_cmd $(proc_file) @Argv } proc pg_set { var dev = $1 var 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 { var result = '' var proc_file = $1 # after shift, the remaining args are contained in $@ shift var 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 var status = $Status set 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 { var result = '' if [[ "$DEBUG" == "yes" ]] { echo "cmd: $1 > $PGDEV" } echo $1 > $PGDEV var status = $Status set 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" } }