functions.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #
  2. # Common functions used by pktgen scripts
  3. # - Depending on bash 3 (or higher) syntax
  4. #
  5. # Author: Jesper Dangaaard Brouer
  6. # License: GPL
  7. ## -- General shell logging cmds --
  8. function err() {
  9. local exitcode=$1
  10. shift
  11. echo "ERROR: $@" >&2
  12. exit $exitcode
  13. }
  14. function warn() {
  15. echo "WARN : $@" >&2
  16. }
  17. function info() {
  18. if [[ -n "$VERBOSE" ]]; then
  19. echo "INFO : $@" >&2
  20. fi
  21. }
  22. ## -- Pktgen proc config commands -- ##
  23. export PROC_DIR=/proc/net/pktgen
  24. #
  25. # Three different shell functions for configuring the different
  26. # components of pktgen:
  27. # pg_ctrl(), pg_thread() and pg_set().
  28. #
  29. # These functions correspond to pktgens different components.
  30. # * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl)
  31. # * pg_thread() control the kernel threads and binding to devices
  32. # * pg_set() control setup of individual devices
  33. function pg_ctrl() {
  34. local proc_file="pgctrl"
  35. proc_cmd ${proc_file} "$@"
  36. }
  37. function pg_thread() {
  38. local thread=$1
  39. local proc_file="kpktgend_${thread}"
  40. shift
  41. proc_cmd ${proc_file} "$@"
  42. }
  43. function pg_set() {
  44. local dev=$1
  45. local proc_file="$dev"
  46. shift
  47. proc_cmd ${proc_file} "$@"
  48. }
  49. # More generic replacement for pgset(), that does not depend on global
  50. # variable for proc file.
  51. function proc_cmd() {
  52. local result
  53. local proc_file=$1
  54. # after shift, the remaining args are contained in $@
  55. shift
  56. local proc_ctrl=${PROC_DIR}/$proc_file
  57. if [[ ! -e "$proc_ctrl" ]]; then
  58. err 3 "proc file:$proc_ctrl does not exists (dev added to thread?)"
  59. else
  60. if [[ ! -w "$proc_ctrl" ]]; then
  61. err 4 "proc file:$proc_ctrl not writable, not root?!"
  62. fi
  63. fi
  64. if [[ "$DEBUG" == "yes" ]]; then
  65. echo "cmd: $@ > $proc_ctrl"
  66. fi
  67. # Quoting of "$@" is important for space expansion
  68. echo "$@" > "$proc_ctrl"
  69. local status=$?
  70. result=$(grep "Result: OK:" $proc_ctrl)
  71. # Due to pgctrl, cannot use exit code $? from grep
  72. if [[ "$result" == "" ]]; then
  73. grep "Result:" $proc_ctrl >&2
  74. fi
  75. if (( $status != 0 )); then
  76. err 5 "Write error($status) occurred cmd: \"$@ > $proc_ctrl\""
  77. fi
  78. }
  79. # Old obsolete "pgset" function, with slightly improved err handling
  80. function pgset() {
  81. local result
  82. if [[ "$DEBUG" == "yes" ]]; then
  83. echo "cmd: $1 > $PGDEV"
  84. fi
  85. echo $1 > $PGDEV
  86. local status=$?
  87. result=`cat $PGDEV | fgrep "Result: OK:"`
  88. if [[ "$result" == "" ]]; then
  89. cat $PGDEV | fgrep Result:
  90. fi
  91. if (( $status != 0 )); then
  92. err 5 "Write error($status) occurred cmd: \"$1 > $PGDEV\""
  93. fi
  94. }
  95. ## -- General shell tricks --
  96. function root_check_run_with_sudo() {
  97. # Trick so, program can be run as normal user, will just use "sudo"
  98. # call as root_check_run_as_sudo "$@"
  99. if [ "$EUID" -ne 0 ]; then
  100. if [ -x $0 ]; then # Directly executable use sudo
  101. info "Not root, running with sudo"
  102. sudo "$0" "$@"
  103. exit $?
  104. fi
  105. err 4 "cannot perform sudo run of $0"
  106. fi
  107. }