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