Path: blob/master/tools/testing/selftests/drivers/net/netdevsim/psample.sh
26299 views
#!/bin/bash1# SPDX-License-Identifier: GPL-2.02#3# This test is for checking the psample module. It makes use of netdevsim4# which periodically generates "sampled" packets.56lib_dir=$(dirname $0)/../../../net/forwarding78ALL_TESTS="9psample_enable_test10psample_group_num_test11psample_md_test12"13NETDEVSIM_PATH=/sys/bus/netdevsim/14DEV_ADDR=133715DEV=netdevsim${DEV_ADDR}16SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV/net/17PSAMPLE_DIR=/sys/kernel/debug/netdevsim/$DEV/psample/18CAPTURE_FILE=$(mktemp)19NUM_NETIFS=020source $lib_dir/lib.sh2122DEVLINK_DEV=23source $lib_dir/devlink_lib.sh24DEVLINK_DEV=netdevsim/${DEV}2526# Available at https://github.com/Mellanox/libpsample27require_command psample2829psample_capture()30{31rm -f $CAPTURE_FILE3233timeout 2 ip netns exec testns1 psample &> $CAPTURE_FILE34}3536psample_enable_test()37{38RET=03940echo 1 > $PSAMPLE_DIR/enable41check_err $? "Failed to enable sampling when should not"4243echo 1 > $PSAMPLE_DIR/enable 2>/dev/null44check_fail $? "Sampling enablement succeeded when should fail"4546psample_capture47if [ $(cat $CAPTURE_FILE | wc -l) -eq 0 ]; then48check_err 1 "Failed to capture sampled packets"49fi5051echo 0 > $PSAMPLE_DIR/enable52check_err $? "Failed to disable sampling when should not"5354echo 0 > $PSAMPLE_DIR/enable 2>/dev/null55check_fail $? "Sampling disablement succeeded when should fail"5657psample_capture58if [ $(cat $CAPTURE_FILE | wc -l) -ne 0 ]; then59check_err 1 "Captured sampled packets when should not"60fi6162log_test "psample enable / disable"63}6465psample_group_num_test()66{67RET=06869echo 1234 > $PSAMPLE_DIR/group_num70echo 1 > $PSAMPLE_DIR/enable7172psample_capture73grep -q -e "group 1234" $CAPTURE_FILE74check_err $? "Sampled packets reported with wrong group number"7576# New group number should only be used after disable / enable.77echo 4321 > $PSAMPLE_DIR/group_num7879psample_capture80grep -q -e "group 4321" $CAPTURE_FILE81check_fail $? "Group number changed while sampling is active"8283echo 0 > $PSAMPLE_DIR/enable && echo 1 > $PSAMPLE_DIR/enable8485psample_capture86grep -q -e "group 4321" $CAPTURE_FILE87check_err $? "Group number did not change after restarting sampling"8889log_test "psample group number"9091echo 0 > $PSAMPLE_DIR/enable92}9394psample_md_test()95{96RET=09798echo 1 > $PSAMPLE_DIR/enable99100echo 1234 > $PSAMPLE_DIR/in_ifindex101echo 4321 > $PSAMPLE_DIR/out_ifindex102psample_capture103104grep -q -e "in-ifindex 1234" $CAPTURE_FILE105check_err $? "Sampled packets reported with wrong in-ifindex"106107grep -q -e "out-ifindex 4321" $CAPTURE_FILE108check_err $? "Sampled packets reported with wrong out-ifindex"109110echo 5 > $PSAMPLE_DIR/out_tc111psample_capture112113grep -q -e "out-tc 5" $CAPTURE_FILE114check_err $? "Sampled packets reported with wrong out-tc"115116echo $((2**16 - 1)) > $PSAMPLE_DIR/out_tc117psample_capture118119grep -q -e "out-tc " $CAPTURE_FILE120check_fail $? "Sampled packets reported with out-tc when should not"121122echo 1 > $PSAMPLE_DIR/out_tc123echo 10000 > $PSAMPLE_DIR/out_tc_occ_max124psample_capture125126grep -q -e "out-tc-occ " $CAPTURE_FILE127check_err $? "Sampled packets not reported with out-tc-occ when should"128129echo 0 > $PSAMPLE_DIR/out_tc_occ_max130psample_capture131132grep -q -e "out-tc-occ " $CAPTURE_FILE133check_fail $? "Sampled packets reported with out-tc-occ when should not"134135echo 10000 > $PSAMPLE_DIR/latency_max136psample_capture137138grep -q -e "latency " $CAPTURE_FILE139check_err $? "Sampled packets not reported with latency when should"140141echo 0 > $PSAMPLE_DIR/latency_max142psample_capture143144grep -q -e "latency " $CAPTURE_FILE145check_fail $? "Sampled packets reported with latency when should not"146147log_test "psample metadata"148149echo 0 > $PSAMPLE_DIR/enable150}151152setup_prepare()153{154modprobe netdevsim &> /dev/null155156echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device157while [ ! -d $SYSFS_NET_DIR ] ; do :; done158159set -e160161ip netns add testns1162devlink dev reload $DEVLINK_DEV netns testns1163164set +e165}166167cleanup()168{169pre_cleanup170rm -f $CAPTURE_FILE171ip netns del testns1172echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device173modprobe -r netdevsim &> /dev/null174}175176trap cleanup EXIT177178setup_prepare179180tests_run181182exit $EXIT_STATUS183184185