selftests: ptp: Add Physical Hardware Clock test
authorShalom Toledo <shalomt@mellanox.com>
Tue, 11 Jun 2019 15:45:12 +0000 (18:45 +0300)
committerDavid S. Miller <davem@davemloft.net>
Fri, 14 Jun 2019 05:34:55 +0000 (22:34 -0700)
Test the PTP Physical Hardware Clock functionality using the "phc_ctl" (a
part of "linuxptp").

The test contains three sub-tests:
  * "settime" test
  * "adjtime" test
  * "adjfreq" test

"settime" test:
  * set the PHC time to 0 seconds.
  * wait for 120.5 seconds.
  * check if PHC time equal to 120.XX seconds.

"adjtime" test:
  * set the PHC time to 0 seconds.
  * adjust the time by 10 seconds.
  * check if PHC time equal to 10.XX seconds.

"adjfreq" test:
  * adjust the PHC frequency to be 1% faster.
  * set the PHC time to 0 seconds.
  * wait for 100.5 seconds.
  * check if PHC time equal to 101.XX seconds.

Usage:
  $ ./phc.sh /dev/ptp<X>

  It is possible to run a subset of the tests, for example:
    * To run only the "settime" test:
      $ TESTS="settime" ./phc.sh /dev/ptp<X>

Signed-off-by: Shalom Toledo <shalomt@mellanox.com>
Reviewed-by: Petr Machata <petrm@mellanox.com>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Tested-by: Vladimir Oltean <olteanv@gmail.com>
Signed-off-by: Ido Schimmel <idosch@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
tools/testing/selftests/ptp/phc.sh [new file with mode: 0755]

diff --git a/tools/testing/selftests/ptp/phc.sh b/tools/testing/selftests/ptp/phc.sh
new file mode 100755 (executable)
index 0000000..ac6e5a6
--- /dev/null
@@ -0,0 +1,166 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+ALL_TESTS="
+       settime
+       adjtime
+       adjfreq
+"
+DEV=$1
+
+##############################################################################
+# Sanity checks
+
+if [[ "$(id -u)" -ne 0 ]]; then
+       echo "SKIP: need root privileges"
+       exit 0
+fi
+
+if [[ "$DEV" == "" ]]; then
+       echo "SKIP: PTP device not provided"
+       exit 0
+fi
+
+require_command()
+{
+       local cmd=$1; shift
+
+       if [[ ! -x "$(command -v "$cmd")" ]]; then
+               echo "SKIP: $cmd not installed"
+               exit 1
+       fi
+}
+
+phc_sanity()
+{
+       phc_ctl $DEV get &> /dev/null
+
+       if [ $? != 0 ]; then
+               echo "SKIP: unknown clock $DEV: No such device"
+               exit 1
+       fi
+}
+
+require_command phc_ctl
+phc_sanity
+
+##############################################################################
+# Helpers
+
+# Exit status to return at the end. Set in case one of the tests fails.
+EXIT_STATUS=0
+# Per-test return value. Clear at the beginning of each test.
+RET=0
+
+check_err()
+{
+       local err=$1
+
+       if [[ $RET -eq 0 && $err -ne 0 ]]; then
+               RET=$err
+       fi
+}
+
+log_test()
+{
+       local test_name=$1
+
+       if [[ $RET -ne 0 ]]; then
+               EXIT_STATUS=1
+               printf "TEST: %-60s  [FAIL]\n" "$test_name"
+               return 1
+       fi
+
+       printf "TEST: %-60s  [ OK ]\n" "$test_name"
+       return 0
+}
+
+tests_run()
+{
+       local current_test
+
+       for current_test in ${TESTS:-$ALL_TESTS}; do
+               $current_test
+       done
+}
+
+##############################################################################
+# Tests
+
+settime_do()
+{
+       local res
+
+       res=$(phc_ctl $DEV set 0 wait 120.5 get 2> /dev/null \
+               | awk '/clock time is/{print $5}' \
+               | awk -F. '{print $1}')
+
+       (( res == 120 ))
+}
+
+adjtime_do()
+{
+       local res
+
+       res=$(phc_ctl $DEV set 0 adj 10 get 2> /dev/null \
+               | awk '/clock time is/{print $5}' \
+               | awk -F. '{print $1}')
+
+       (( res == 10 ))
+}
+
+adjfreq_do()
+{
+       local res
+
+       # Set the clock to be 1% faster
+       res=$(phc_ctl $DEV freq 10000000 set 0 wait 100.5 get 2> /dev/null \
+               | awk '/clock time is/{print $5}' \
+               | awk -F. '{print $1}')
+
+       (( res == 101 ))
+}
+
+##############################################################################
+
+cleanup()
+{
+       phc_ctl $DEV freq 0.0 &> /dev/null
+       phc_ctl $DEV set &> /dev/null
+}
+
+settime()
+{
+       RET=0
+
+       settime_do
+       check_err $?
+       log_test "settime"
+       cleanup
+}
+
+adjtime()
+{
+       RET=0
+
+       adjtime_do
+       check_err $?
+       log_test "adjtime"
+       cleanup
+}
+
+adjfreq()
+{
+       RET=0
+
+       adjfreq_do
+       check_err $?
+       log_test "adjfreq"
+       cleanup
+}
+
+trap cleanup EXIT
+
+tests_run
+
+exit $EXIT_STATUS