0001
0002
0003
0004
0005
0006
0007 lib_dir=$(dirname $0)/../../../net/forwarding
0008
0009 ALL_TESTS="
0010 psample_enable_test
0011 psample_group_num_test
0012 psample_md_test
0013 "
0014 NETDEVSIM_PATH=/sys/bus/netdevsim/
0015 DEV_ADDR=1337
0016 DEV=netdevsim${DEV_ADDR}
0017 SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/
0018 PSAMPLE_DIR=/sys/kernel/debug/netdevsim/$DEV/psample/
0019 CAPTURE_FILE=$(mktemp)
0020 NUM_NETIFS=0
0021 source $lib_dir/lib.sh
0022
0023 DEVLINK_DEV=
0024 source $lib_dir/devlink_lib.sh
0025 DEVLINK_DEV=netdevsim/${DEV}
0026
0027
0028 require_command psample
0029
0030 psample_capture()
0031 {
0032 rm -f $CAPTURE_FILE
0033
0034 timeout 2 ip netns exec testns1 psample &> $CAPTURE_FILE
0035 }
0036
0037 psample_enable_test()
0038 {
0039 RET=0
0040
0041 echo 1 > $PSAMPLE_DIR/enable
0042 check_err $? "Failed to enable sampling when should not"
0043
0044 echo 1 > $PSAMPLE_DIR/enable 2>/dev/null
0045 check_fail $? "Sampling enablement succeeded when should fail"
0046
0047 psample_capture
0048 if [ $(cat $CAPTURE_FILE | wc -l) -eq 0 ]; then
0049 check_err 1 "Failed to capture sampled packets"
0050 fi
0051
0052 echo 0 > $PSAMPLE_DIR/enable
0053 check_err $? "Failed to disable sampling when should not"
0054
0055 echo 0 > $PSAMPLE_DIR/enable 2>/dev/null
0056 check_fail $? "Sampling disablement succeeded when should fail"
0057
0058 psample_capture
0059 if [ $(cat $CAPTURE_FILE | wc -l) -ne 0 ]; then
0060 check_err 1 "Captured sampled packets when should not"
0061 fi
0062
0063 log_test "psample enable / disable"
0064 }
0065
0066 psample_group_num_test()
0067 {
0068 RET=0
0069
0070 echo 1234 > $PSAMPLE_DIR/group_num
0071 echo 1 > $PSAMPLE_DIR/enable
0072
0073 psample_capture
0074 grep -q -e "group 1234" $CAPTURE_FILE
0075 check_err $? "Sampled packets reported with wrong group number"
0076
0077
0078 echo 4321 > $PSAMPLE_DIR/group_num
0079
0080 psample_capture
0081 grep -q -e "group 4321" $CAPTURE_FILE
0082 check_fail $? "Group number changed while sampling is active"
0083
0084 echo 0 > $PSAMPLE_DIR/enable && echo 1 > $PSAMPLE_DIR/enable
0085
0086 psample_capture
0087 grep -q -e "group 4321" $CAPTURE_FILE
0088 check_err $? "Group number did not change after restarting sampling"
0089
0090 log_test "psample group number"
0091
0092 echo 0 > $PSAMPLE_DIR/enable
0093 }
0094
0095 psample_md_test()
0096 {
0097 RET=0
0098
0099 echo 1 > $PSAMPLE_DIR/enable
0100
0101 echo 1234 > $PSAMPLE_DIR/in_ifindex
0102 echo 4321 > $PSAMPLE_DIR/out_ifindex
0103 psample_capture
0104
0105 grep -q -e "in-ifindex 1234" $CAPTURE_FILE
0106 check_err $? "Sampled packets reported with wrong in-ifindex"
0107
0108 grep -q -e "out-ifindex 4321" $CAPTURE_FILE
0109 check_err $? "Sampled packets reported with wrong out-ifindex"
0110
0111 echo 5 > $PSAMPLE_DIR/out_tc
0112 psample_capture
0113
0114 grep -q -e "out-tc 5" $CAPTURE_FILE
0115 check_err $? "Sampled packets reported with wrong out-tc"
0116
0117 echo $((2**16 - 1)) > $PSAMPLE_DIR/out_tc
0118 psample_capture
0119
0120 grep -q -e "out-tc " $CAPTURE_FILE
0121 check_fail $? "Sampled packets reported with out-tc when should not"
0122
0123 echo 1 > $PSAMPLE_DIR/out_tc
0124 echo 10000 > $PSAMPLE_DIR/out_tc_occ_max
0125 psample_capture
0126
0127 grep -q -e "out-tc-occ " $CAPTURE_FILE
0128 check_err $? "Sampled packets not reported with out-tc-occ when should"
0129
0130 echo 0 > $PSAMPLE_DIR/out_tc_occ_max
0131 psample_capture
0132
0133 grep -q -e "out-tc-occ " $CAPTURE_FILE
0134 check_fail $? "Sampled packets reported with out-tc-occ when should not"
0135
0136 echo 10000 > $PSAMPLE_DIR/latency_max
0137 psample_capture
0138
0139 grep -q -e "latency " $CAPTURE_FILE
0140 check_err $? "Sampled packets not reported with latency when should"
0141
0142 echo 0 > $PSAMPLE_DIR/latency_max
0143 psample_capture
0144
0145 grep -q -e "latency " $CAPTURE_FILE
0146 check_fail $? "Sampled packets reported with latency when should not"
0147
0148 log_test "psample metadata"
0149
0150 echo 0 > $PSAMPLE_DIR/enable
0151 }
0152
0153 setup_prepare()
0154 {
0155 modprobe netdevsim &> /dev/null
0156
0157 echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
0158 while [ ! -d $SYSFS_NET_DIR ] ; do :; done
0159
0160 set -e
0161
0162 ip netns add testns1
0163 devlink dev reload $DEVLINK_DEV netns testns1
0164
0165 set +e
0166 }
0167
0168 cleanup()
0169 {
0170 pre_cleanup
0171 rm -f $CAPTURE_FILE
0172 ip netns del testns1
0173 echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
0174 modprobe -r netdevsim &> /dev/null
0175 }
0176
0177 trap cleanup EXIT
0178
0179 setup_prepare
0180
0181 tests_run
0182
0183 exit $EXIT_STATUS