This commit adds a new kernel selftest to verify RTNLGRP_IPV4_MCADDR
and RTNLGRP_IPV6_MCADDR notifications. The test works by adding and
removing a dummy interface and then confirming that the system
correctly receives join and removal notifications for the 224.0.0.1
and ff02::1 multicast addresses.

The test relies on the iproute2 version to be 6.13+.

Tested by the following command:
$ vng -v --user root --cpus 16 -- \
make -C tools/testing/selftests TARGETS=net
TEST_PROGS=rtnetlink_notification.sh \
TEST_GEN_PROGS="" run_tests

Cc: Maciej Żenczykowski <m...@google.com>
Cc: Lorenzo Colitti <lore...@google.com>
Signed-off-by: Yuyang Huang <yuyanghu...@google.com>
---

Changelog since v2:
- Move the test case to a separate file.

Changelog since v1:
- Skip the test if the iproute2 is too old.

 tools/testing/selftests/net/Makefile          |   1 +
 .../selftests/net/rtnetlink_notification.sh   | 159 ++++++++++++++++++
 2 files changed, 160 insertions(+)
 create mode 100755 tools/testing/selftests/net/rtnetlink_notification.sh

diff --git a/tools/testing/selftests/net/Makefile 
b/tools/testing/selftests/net/Makefile
index 70a38f485d4d..ad258b25bc9d 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -40,6 +40,7 @@ TEST_PROGS += netns-name.sh
 TEST_PROGS += link_netns.py
 TEST_PROGS += nl_netdev.py
 TEST_PROGS += rtnetlink.py
+TEST_PROGS += rtnetlink_notification.sh
 TEST_PROGS += srv6_end_dt46_l3vpn_test.sh
 TEST_PROGS += srv6_end_dt4_l3vpn_test.sh
 TEST_PROGS += srv6_end_dt6_l3vpn_test.sh
diff --git a/tools/testing/selftests/net/rtnetlink_notification.sh 
b/tools/testing/selftests/net/rtnetlink_notification.sh
new file mode 100755
index 000000000000..a2c1afed5023
--- /dev/null
+++ b/tools/testing/selftests/net/rtnetlink_notification.sh
@@ -0,0 +1,159 @@
+#!/bin/bash
+#
+# This test is for checking rtnetlink notification callpaths, and get as much
+# coverage as possible.
+#
+# set -e
+
+ALL_TESTS="
+       kci_test_mcast_addr_notification
+"
+
+VERBOSE=0
+PAUSE=no
+PAUSE_ON_FAIL=no
+
+source lib.sh
+
+# set global exit status, but never reset nonzero one.
+check_err()
+{
+       if [ $ret -eq 0 ]; then
+               ret=$1
+       fi
+       [ -n "$2" ] && echo "$2"
+}
+
+run_cmd_common()
+{
+       local cmd="$*"
+       local out
+       if [ "$VERBOSE" = "1" ]; then
+               echo "COMMAND: ${cmd}"
+       fi
+       out=$($cmd 2>&1)
+       rc=$?
+       if [ "$VERBOSE" = "1" -a -n "$out" ]; then
+               echo "    $out"
+       fi
+       return $rc
+}
+
+run_cmd() {
+       run_cmd_common "$@"
+       rc=$?
+       check_err $rc
+       return $rc
+}
+
+end_test()
+{
+       echo "$*"
+       [ "${VERBOSE}" = "1" ] && echo
+
+       if [[ $ret -ne 0 ]] && [[ "${PAUSE_ON_FAIL}" = "yes" ]]; then
+               echo "Hit enter to continue"
+               read a
+       fi;
+
+       if [ "${PAUSE}" = "yes" ]; then
+               echo "Hit enter to continue"
+               read a
+       fi
+
+}
+
+kci_test_mcast_addr_notification()
+{
+       local tmpfile
+       local monitor_pid
+       local match_result
+
+       tmpfile=$(mktemp)
+
+       ip monitor maddr > $tmpfile &
+       monitor_pid=$!
+       sleep 1
+       if [ ! -e "/proc/$monitor_pid" ]; then
+               end_test "SKIP: mcast addr notification: iproute2 too old"
+               rm $tmpfile
+               return $ksft_skip
+       fi
+
+       run_cmd ip link add name test-dummy1 type dummy
+       run_cmd ip link set test-dummy1 up
+       run_cmd ip link del dev test-dummy1
+       sleep 1
+
+       match_result=$(grep -cE "test-dummy1.*(224.0.0.1|ff02::1)" $tmpfile)
+
+       kill $monitor_pid
+       rm $tmpfile
+       # There should be 4 line matches as follows.
+       # 13: test-dummy1    inet6 mcast ff02::1 scope global 
+       # 13: test-dummy1    inet mcast 224.0.0.1 scope global 
+       # Deleted 13: test-dummy1    inet mcast 224.0.0.1 scope global 
+       # Deleted 13: test-dummy1    inet6 mcast ff02::1 scope global 
+       if [ $match_result -ne 4 ];then
+               end_test "FAIL: mcast addr notification"
+               return 1
+       fi
+       end_test "PASS: mcast addr notification"
+}
+
+kci_test_rtnl()
+{
+       local current_test
+       local ret=0
+
+       for current_test in ${TESTS:-$ALL_TESTS}; do
+               $current_test
+               check_err $?
+       done
+
+       return $ret
+}
+
+usage()
+{
+       cat <<EOF
+usage: ${0##*/} OPTS
+
+        -t <test>   Test(s) to run (default: all)
+                    (options: $(echo $ALL_TESTS))
+        -v          Verbose mode (show commands and output)
+        -P          Pause after every test
+        -p          Pause after every failing test before cleanup (for 
debugging)
+EOF
+}
+
+#check for needed privileges
+if [ "$(id -u)" -ne 0 ];then
+       end_test "SKIP: Need root privileges"
+       exit $ksft_skip
+fi
+
+for x in ip;do
+       $x -Version 2>/dev/null >/dev/null
+       if [ $? -ne 0 ];then
+               end_test "SKIP: Could not run test without the $x tool"
+               exit $ksft_skip
+       fi
+done
+
+while getopts t:hvpP o; do
+       case $o in
+               t) TESTS=$OPTARG;;
+               v) VERBOSE=1;;
+               p) PAUSE_ON_FAIL=yes;;
+               P) PAUSE=yes;;
+               h) usage; exit 0;;
+               *) usage; exit 1;;
+       esac
+done
+
+[ $PAUSE = "yes" ] && PAUSE_ON_FAIL="no"
+
+kci_test_rtnl
+
+exit $?
-- 
2.50.0.rc1.591.g9c95f17f64-goog


Reply via email to