functions.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. set -o errexit
  8. ## -- General shell logging cmds --
  9. function err() {
  10. local exitcode=$1
  11. shift
  12. echo "ERROR: $@" >&2
  13. exit $exitcode
  14. }
  15. function warn() {
  16. echo "WARN : $@" >&2
  17. }
  18. function info() {
  19. if [[ -n "$VERBOSE" ]]; then
  20. echo "INFO : $@" >&2
  21. fi
  22. }
  23. ## -- Pktgen proc config commands -- ##
  24. export PROC_DIR=/proc/net/pktgen
  25. #
  26. # Three different shell functions for configuring the different
  27. # components of pktgen:
  28. # pg_ctrl(), pg_thread() and pg_set().
  29. #
  30. # These functions correspond to pktgens different components.
  31. # * pg_ctrl() control "pgctrl" (/proc/net/pktgen/pgctrl)
  32. # * pg_thread() control the kernel threads and binding to devices
  33. # * pg_set() control setup of individual devices
  34. function pg_ctrl() {
  35. local proc_file="pgctrl"
  36. proc_cmd ${proc_file} "$@"
  37. }
  38. function pg_thread() {
  39. local thread=$1
  40. local proc_file="kpktgend_${thread}"
  41. shift
  42. proc_cmd ${proc_file} "$@"
  43. }
  44. function pg_set() {
  45. local dev=$1
  46. local proc_file="$dev"
  47. shift
  48. proc_cmd ${proc_file} "$@"
  49. }
  50. # More generic replacement for pgset(), that does not depend on global
  51. # variable for proc file.
  52. function proc_cmd() {
  53. local result
  54. local proc_file=$1
  55. local status=0
  56. # after shift, the remaining args are contained in $@
  57. shift
  58. local proc_ctrl=${PROC_DIR}/$proc_file
  59. if [[ ! -e "$proc_ctrl" ]]; then
  60. err 3 "proc file:$proc_ctrl does not exists (dev added to thread?)"
  61. else
  62. if [[ ! -w "$proc_ctrl" ]]; then
  63. err 4 "proc file:$proc_ctrl not writable, not root?!"
  64. fi
  65. fi
  66. if [[ "$DEBUG" == "yes" ]]; then
  67. echo "cmd: $@ > $proc_ctrl"
  68. fi
  69. # Quoting of "$@" is important for space expansion
  70. echo "$@" > "$proc_ctrl" || status=$?
  71. if [[ "$proc_file" != "pgctrl" ]]; then
  72. result=$(grep "Result: OK:" $proc_ctrl) || true
  73. if [[ "$result" == "" ]]; then
  74. grep "Result:" $proc_ctrl >&2
  75. fi
  76. fi
  77. if (( $status != 0 )); then
  78. err 5 "Write error($status) occurred cmd: \"$@ > $proc_ctrl\""
  79. fi
  80. }
  81. # Old obsolete "pgset" function, with slightly improved err handling
  82. function pgset() {
  83. local result
  84. if [[ "$DEBUG" == "yes" ]]; then
  85. echo "cmd: $1 > $PGDEV"
  86. fi
  87. echo $1 > $PGDEV
  88. local status=$?
  89. result=`cat $PGDEV | fgrep "Result: OK:"`
  90. if [[ "$result" == "" ]]; then
  91. cat $PGDEV | fgrep Result:
  92. fi
  93. if (( $status != 0 )); then
  94. err 5 "Write error($status) occurred cmd: \"$1 > $PGDEV\""
  95. fi
  96. }
  97. [[ $EUID -eq 0 ]] && trap 'pg_ctrl "reset"' EXIT
  98. ## -- General shell tricks --
  99. function root_check_run_with_sudo() {
  100. # Trick so, program can be run as normal user, will just use "sudo"
  101. # call as root_check_run_as_sudo "$@"
  102. if [ "$EUID" -ne 0 ]; then
  103. if [ -x $0 ]; then # Directly executable use sudo
  104. info "Not root, running with sudo"
  105. sudo "$0" "$@"
  106. exit $?
  107. fi
  108. err 4 "cannot perform sudo run of $0"
  109. fi
  110. }
  111. # Exact input device's NUMA node info
  112. function get_iface_node()
  113. {
  114. local node=$(</sys/class/net/$1/device/numa_node)
  115. if [[ $node == -1 ]]; then
  116. echo 0
  117. else
  118. echo $node
  119. fi
  120. }
  121. # Given an Dev/iface, get its queues' irq numbers
  122. function get_iface_irqs()
  123. {
  124. local IFACE=$1
  125. local queues="${IFACE}-.*TxRx"
  126. irqs=$(grep "$queues" /proc/interrupts | cut -f1 -d:)
  127. [ -z "$irqs" ] && irqs=$(grep $IFACE /proc/interrupts | cut -f1 -d:)
  128. [ -z "$irqs" ] && irqs=$(for i in `ls -Ux /sys/class/net/$IFACE/device/msi_irqs` ;\
  129. do grep "$i:.*TxRx" /proc/interrupts | grep -v fdir | cut -f 1 -d : ;\
  130. done)
  131. [ -z "$irqs" ] && err 3 "Could not find interrupts for $IFACE"
  132. echo $irqs
  133. }
  134. # Given a NUMA node, return cpu ids belonging to it.
  135. function get_node_cpus()
  136. {
  137. local node=$1
  138. local node_cpu_list
  139. local node_cpu_range_list=`cut -f1- -d, --output-delimiter=" " \
  140. /sys/devices/system/node/node$node/cpulist`
  141. for cpu_range in $node_cpu_range_list
  142. do
  143. node_cpu_list="$node_cpu_list "`seq -s " " ${cpu_range//-/ }`
  144. done
  145. echo $node_cpu_list
  146. }