Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

testptp: add option to shift clock by nanoseconds

Add option to shift the clock by a specified number of nanoseconds.

The new argument -n will specify the number of nanoseconds to add to the
ptp clock. Since the API doesn't support negative shifts those needs to
be calculated by subtracting full seconds and adding a nanosecond offset.

Signed-off-by: Maciek Machnikowski <maciek@machnikowski.net>
Acked-by: Richard Cochran <richardcochran@gmail.com>
Link: https://lore.kernel.org/r/20220221200637.125595-1-maciek@machnikowski.net
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Maciek Machnikowski and committed by
Jakub Kicinski
f64ae40d cc727b64

+14 -4
+14 -4
tools/testing/selftests/ptp/testptp.c
··· 133 133 " 0 - none\n" 134 134 " 1 - external time stamp\n" 135 135 " 2 - periodic output\n" 136 + " -n val shift the ptp clock time by 'val' nanoseconds\n" 136 137 " -p val enable output with a period of 'val' nanoseconds\n" 137 138 " -H val set output phase to 'val' nanoseconds (requires -p)\n" 138 139 " -w val set output pulse width to 'val' nanoseconds (requires -p)\n" ··· 166 165 clockid_t clkid; 167 166 int adjfreq = 0x7fffffff; 168 167 int adjtime = 0; 168 + int adjns = 0; 169 169 int capabilities = 0; 170 170 int extts = 0; 171 171 int flagtest = 0; ··· 188 186 189 187 progname = strrchr(argv[0], '/'); 190 188 progname = progname ? 1+progname : argv[0]; 191 - while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:p:P:sSt:T:w:z"))) { 189 + while (EOF != (c = getopt(argc, argv, "cd:e:f:ghH:i:k:lL:n:p:P:sSt:T:w:z"))) { 192 190 switch (c) { 193 191 case 'c': 194 192 capabilities = 1; ··· 224 222 usage(progname); 225 223 return -1; 226 224 } 225 + break; 226 + case 'n': 227 + adjns = atoi(optarg); 227 228 break; 228 229 case 'p': 229 230 perout = atoll(optarg); ··· 310 305 } 311 306 } 312 307 313 - if (adjtime) { 308 + if (adjtime || adjns) { 314 309 memset(&tx, 0, sizeof(tx)); 315 - tx.modes = ADJ_SETOFFSET; 310 + tx.modes = ADJ_SETOFFSET | ADJ_NANO; 316 311 tx.time.tv_sec = adjtime; 317 - tx.time.tv_usec = 0; 312 + tx.time.tv_usec = adjns; 313 + while (tx.time.tv_usec < 0) { 314 + tx.time.tv_sec -= 1; 315 + tx.time.tv_usec += 1000000000; 316 + } 317 + 318 318 if (clock_adjtime(clkid, &tx) < 0) { 319 319 perror("clock_adjtime"); 320 320 } else {