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