ee10b1a8933c44c9deabdf5008ad415e86419d0e
[linux-2.6-microblaze.git] / tools / testing / selftests / drivers / net / netdevsim / psample.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 #
4 # This test is for checking the psample module. It makes use of netdevsim
5 # which periodically generates "sampled" packets.
6
7 lib_dir=$(dirname $0)/../../../net/forwarding
8
9 ALL_TESTS="
10         psample_enable_test
11         psample_group_num_test
12         psample_md_test
13 "
14 NETDEVSIM_PATH=/sys/bus/netdevsim/
15 DEV_ADDR=1337
16 DEV=netdevsim${DEV_ADDR}
17 DEVLINK_DEV=netdevsim/${DEV}
18 SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/
19 PSAMPLE_DIR=/sys/kernel/debug/netdevsim/$DEV/psample/
20 CAPTURE_FILE=$(mktemp)
21 NUM_NETIFS=0
22 source $lib_dir/lib.sh
23 source $lib_dir/devlink_lib.sh
24
25 # Available at https://github.com/Mellanox/libpsample
26 require_command psample
27
28 psample_capture()
29 {
30         rm -f $CAPTURE_FILE
31
32         timeout 2 ip netns exec testns1 psample &> $CAPTURE_FILE
33 }
34
35 psample_enable_test()
36 {
37         RET=0
38
39         echo 1 > $PSAMPLE_DIR/enable
40         check_err $? "Failed to enable sampling when should not"
41
42         echo 1 > $PSAMPLE_DIR/enable 2>/dev/null
43         check_fail $? "Sampling enablement succeeded when should fail"
44
45         psample_capture
46         if [ $(cat $CAPTURE_FILE | wc -l) -eq 0 ]; then
47                 check_err 1 "Failed to capture sampled packets"
48         fi
49
50         echo 0 > $PSAMPLE_DIR/enable
51         check_err $? "Failed to disable sampling when should not"
52
53         echo 0 > $PSAMPLE_DIR/enable 2>/dev/null
54         check_fail $? "Sampling disablement succeeded when should fail"
55
56         psample_capture
57         if [ $(cat $CAPTURE_FILE | wc -l) -ne 0 ]; then
58                 check_err 1 "Captured sampled packets when should not"
59         fi
60
61         log_test "psample enable / disable"
62 }
63
64 psample_group_num_test()
65 {
66         RET=0
67
68         echo 1234 > $PSAMPLE_DIR/group_num
69         echo 1 > $PSAMPLE_DIR/enable
70
71         psample_capture
72         grep -q -e "group 1234" $CAPTURE_FILE
73         check_err $? "Sampled packets reported with wrong group number"
74
75         # New group number should only be used after disable / enable.
76         echo 4321 > $PSAMPLE_DIR/group_num
77
78         psample_capture
79         grep -q -e "group 4321" $CAPTURE_FILE
80         check_fail $? "Group number changed while sampling is active"
81
82         echo 0 > $PSAMPLE_DIR/enable && echo 1 > $PSAMPLE_DIR/enable
83
84         psample_capture
85         grep -q -e "group 4321" $CAPTURE_FILE
86         check_err $? "Group number did not change after restarting sampling"
87
88         log_test "psample group number"
89
90         echo 0 > $PSAMPLE_DIR/enable
91 }
92
93 psample_md_test()
94 {
95         RET=0
96
97         echo 1 > $PSAMPLE_DIR/enable
98
99         echo 1234 > $PSAMPLE_DIR/in_ifindex
100         echo 4321 > $PSAMPLE_DIR/out_ifindex
101         psample_capture
102
103         grep -q -e "in-ifindex 1234" $CAPTURE_FILE
104         check_err $? "Sampled packets reported with wrong in-ifindex"
105
106         grep -q -e "out-ifindex 4321" $CAPTURE_FILE
107         check_err $? "Sampled packets reported with wrong out-ifindex"
108
109         echo 5 > $PSAMPLE_DIR/out_tc
110         psample_capture
111
112         grep -q -e "out-tc 5" $CAPTURE_FILE
113         check_err $? "Sampled packets reported with wrong out-tc"
114
115         echo $((2**16 - 1)) > $PSAMPLE_DIR/out_tc
116         psample_capture
117
118         grep -q -e "out-tc " $CAPTURE_FILE
119         check_fail $? "Sampled packets reported with out-tc when should not"
120
121         echo 1 > $PSAMPLE_DIR/out_tc
122         echo 10000 > $PSAMPLE_DIR/out_tc_occ_max
123         psample_capture
124
125         grep -q -e "out-tc-occ " $CAPTURE_FILE
126         check_err $? "Sampled packets not reported with out-tc-occ when should"
127
128         echo 0 > $PSAMPLE_DIR/out_tc_occ_max
129         psample_capture
130
131         grep -q -e "out-tc-occ " $CAPTURE_FILE
132         check_fail $? "Sampled packets reported with out-tc-occ when should not"
133
134         echo 10000 > $PSAMPLE_DIR/latency_max
135         psample_capture
136
137         grep -q -e "latency " $CAPTURE_FILE
138         check_err $? "Sampled packets not reported with latency when should"
139
140         echo 0 > $PSAMPLE_DIR/latency_max
141         psample_capture
142
143         grep -q -e "latency " $CAPTURE_FILE
144         check_fail $? "Sampled packets reported with latency when should not"
145
146         log_test "psample metadata"
147
148         echo 0 > $PSAMPLE_DIR/enable
149 }
150
151 setup_prepare()
152 {
153         modprobe netdevsim &> /dev/null
154
155         echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
156         while [ ! -d $SYSFS_NET_DIR ] ; do :; done
157
158         set -e
159
160         ip netns add testns1
161         devlink dev reload $DEVLINK_DEV netns testns1
162
163         set +e
164 }
165
166 cleanup()
167 {
168         pre_cleanup
169         rm -f $CAPTURE_FILE
170         ip netns del testns1
171         echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
172         modprobe -r netdevsim &> /dev/null
173 }
174
175 trap cleanup EXIT
176
177 setup_prepare
178
179 tests_run
180
181 exit $EXIT_STATUS