Skip to content

Commit cda261f

Browse files
wdebruijdavem330
authored andcommitted
selftests: add txtimestamp kselftest
Run the transmit timestamp tests as part of kselftests. Add a txtimestamp.sh test script that runs most variants: ipv4/ipv6, tcp/udp/raw/raw_ipproto/pf_packet, data/nodata, setsockopt/cmsg. The script runs tests with netem delays. Refine txtimestamp.c to validate results. Take expected netem delays as input and compare against real timestamps. To run without dependencies, add a listener socket to be able to connect in the case of TCP. Add the timestamping directory to the kselftests Makefile. Build all the binaries. Only run verified txtimestamp.sh. Signed-off-by: Willem de Bruijn <[email protected]> Acked-by: Soheil Hassas Yeganeh <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b52354a commit cda261f

File tree

5 files changed

+173
-28
lines changed

5 files changed

+173
-28
lines changed

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ TARGETS += mount
2525
TARGETS += mqueue
2626
TARGETS += net
2727
TARGETS += netfilter
28+
TARGETS += networking/timestamping
2829
TARGETS += nsfs
2930
TARGETS += powerpc
3031
TARGETS += proc
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# SPDX-License-Identifier: GPL-2.0
22
CFLAGS += -I../../../../../usr/include
33

4-
TEST_PROGS := hwtstamp_config rxtimestamp timestamping txtimestamp
4+
TEST_GEN_FILES := hwtstamp_config rxtimestamp timestamping txtimestamp
5+
TEST_PROGS := txtimestamp.sh
56

67
all: $(TEST_PROGS)
78

89
top_srcdir = ../../../../..
910
include ../../lib.mk
1011

1112
clean:
12-
rm -fr $(TEST_PROGS)
13+
rm -fr $(TEST_GEN_FILES)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_IFB=y
2+
CONFIG_NET_SCH_NETEM=y

tools/testing/selftests/networking/timestamping/txtimestamp.c

Lines changed: 110 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,67 @@ static int do_ipv4 = 1;
7070
static int do_ipv6 = 1;
7171
static int cfg_payload_len = 10;
7272
static int cfg_poll_timeout = 100;
73+
static int cfg_delay_snd;
74+
static int cfg_delay_ack;
7375
static bool cfg_show_payload;
7476
static bool cfg_do_pktinfo;
7577
static bool cfg_loop_nodata;
7678
static bool cfg_no_delay;
7779
static bool cfg_use_cmsg;
7880
static bool cfg_use_pf_packet;
81+
static bool cfg_do_listen;
7982
static uint16_t dest_port = 9000;
8083

8184
static struct sockaddr_in daddr;
8285
static struct sockaddr_in6 daddr6;
83-
static struct timespec ts_prev;
86+
static struct timespec ts_usr;
87+
88+
static int saved_tskey = -1;
89+
static int saved_tskey_type = -1;
90+
91+
static bool test_failed;
92+
93+
static int64_t timespec_to_us64(struct timespec *ts)
94+
{
95+
return ts->tv_sec * 1000 * 1000 + ts->tv_nsec / 1000;
96+
}
97+
98+
static void validate_key(int tskey, int tstype)
99+
{
100+
int stepsize;
101+
102+
/* compare key for each subsequent request
103+
* must only test for one type, the first one requested
104+
*/
105+
if (saved_tskey == -1)
106+
saved_tskey_type = tstype;
107+
else if (saved_tskey_type != tstype)
108+
return;
109+
110+
stepsize = cfg_proto == SOCK_STREAM ? cfg_payload_len : 1;
111+
if (tskey != saved_tskey + stepsize) {
112+
fprintf(stderr, "ERROR: key %d, expected %d\n",
113+
tskey, saved_tskey + stepsize);
114+
test_failed = true;
115+
}
116+
117+
saved_tskey = tskey;
118+
}
119+
120+
static void validate_timestamp(struct timespec *cur, int min_delay)
121+
{
122+
int max_delay = min_delay + 500 /* processing time upper bound */;
123+
int64_t cur64, start64;
124+
125+
cur64 = timespec_to_us64(cur);
126+
start64 = timespec_to_us64(&ts_usr);
127+
128+
if (cur64 < start64 + min_delay || cur64 > start64 + max_delay) {
129+
fprintf(stderr, "ERROR: delay %lu expected between %d and %d\n",
130+
cur64 - start64, min_delay, max_delay);
131+
test_failed = true;
132+
}
133+
}
84134

85135
static void __print_timestamp(const char *name, struct timespec *cur,
86136
uint32_t key, int payload_len)
@@ -92,48 +142,40 @@ static void __print_timestamp(const char *name, struct timespec *cur,
92142
name, cur->tv_sec, cur->tv_nsec / 1000,
93143
key, payload_len);
94144

95-
if ((ts_prev.tv_sec | ts_prev.tv_nsec)) {
96-
int64_t cur_ms, prev_ms;
97-
98-
cur_ms = (long) cur->tv_sec * 1000 * 1000;
99-
cur_ms += cur->tv_nsec / 1000;
100-
101-
prev_ms = (long) ts_prev.tv_sec * 1000 * 1000;
102-
prev_ms += ts_prev.tv_nsec / 1000;
145+
if (cur != &ts_usr)
146+
fprintf(stderr, " (USR %+" PRId64 " us)",
147+
timespec_to_us64(cur) - timespec_to_us64(&ts_usr));
103148

104-
fprintf(stderr, " (%+" PRId64 " us)", cur_ms - prev_ms);
105-
}
106-
107-
ts_prev = *cur;
108149
fprintf(stderr, "\n");
109150
}
110151

111152
static void print_timestamp_usr(void)
112153
{
113-
struct timespec ts;
114-
struct timeval tv; /* avoid dependency on -lrt */
115-
116-
gettimeofday(&tv, NULL);
117-
ts.tv_sec = tv.tv_sec;
118-
ts.tv_nsec = tv.tv_usec * 1000;
154+
if (clock_gettime(CLOCK_REALTIME, &ts_usr))
155+
error(1, errno, "clock_gettime");
119156

120-
__print_timestamp(" USR", &ts, 0, 0);
157+
__print_timestamp(" USR", &ts_usr, 0, 0);
121158
}
122159

123160
static void print_timestamp(struct scm_timestamping *tss, int tstype,
124161
int tskey, int payload_len)
125162
{
126163
const char *tsname;
127164

165+
validate_key(tskey, tstype);
166+
128167
switch (tstype) {
129168
case SCM_TSTAMP_SCHED:
130169
tsname = " ENQ";
170+
validate_timestamp(&tss->ts[0], 0);
131171
break;
132172
case SCM_TSTAMP_SND:
133173
tsname = " SND";
174+
validate_timestamp(&tss->ts[0], cfg_delay_snd);
134175
break;
135176
case SCM_TSTAMP_ACK:
136177
tsname = " ACK";
178+
validate_timestamp(&tss->ts[0], cfg_delay_ack);
137179
break;
138180
default:
139181
error(1, 0, "unknown timestamp type: %u",
@@ -389,6 +431,9 @@ static void do_test(int family, unsigned int report_opt)
389431
if (fd < 0)
390432
error(1, errno, "socket");
391433

434+
/* reset expected key on each new socket */
435+
saved_tskey = -1;
436+
392437
if (cfg_proto == SOCK_STREAM) {
393438
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
394439
(char*) &val, sizeof(val)))
@@ -431,7 +476,6 @@ static void do_test(int family, unsigned int report_opt)
431476

432477
for (i = 0; i < cfg_num_pkts; i++) {
433478
memset(&msg, 0, sizeof(msg));
434-
memset(&ts_prev, 0, sizeof(ts_prev));
435479
memset(buf, 'a' + i, total_len);
436480

437481
if (cfg_use_pf_packet || cfg_proto == SOCK_RAW) {
@@ -506,7 +550,7 @@ static void do_test(int family, unsigned int report_opt)
506550
error(1, errno, "close");
507551

508552
free(buf);
509-
usleep(400 * 1000);
553+
usleep(100 * 1000);
510554
}
511555

512556
static void __attribute__((noreturn)) usage(const char *filepath)
@@ -522,12 +566,15 @@ static void __attribute__((noreturn)) usage(const char *filepath)
522566
" -F: poll() waits forever for an event\n"
523567
" -I: request PKTINFO\n"
524568
" -l N: send N bytes at a time\n"
569+
" -L listen on hostname and port\n"
525570
" -n: set no-payload option\n"
526571
" -p N: connect to port N\n"
527572
" -P: use PF_PACKET\n"
528573
" -r: use raw\n"
529574
" -R: use raw (IP_HDRINCL)\n"
530575
" -u: use udp\n"
576+
" -v: validate SND delay (usec)\n"
577+
" -V: validate ACK delay (usec)\n"
531578
" -x: show payload (up to 70 bytes)\n",
532579
filepath);
533580
exit(1);
@@ -538,7 +585,7 @@ static void parse_opt(int argc, char **argv)
538585
int proto_count = 0;
539586
int c;
540587

541-
while ((c = getopt(argc, argv, "46c:CDFhIl:np:PrRux")) != -1) {
588+
while ((c = getopt(argc, argv, "46c:CDFhIl:Lnp:PrRuv:V:x")) != -1) {
542589
switch (c) {
543590
case '4':
544591
do_ipv6 = 0;
@@ -564,6 +611,9 @@ static void parse_opt(int argc, char **argv)
564611
case 'l':
565612
cfg_payload_len = strtoul(optarg, NULL, 10);
566613
break;
614+
case 'L':
615+
cfg_do_listen = true;
616+
break;
567617
case 'n':
568618
cfg_loop_nodata = true;
569619
break;
@@ -591,6 +641,12 @@ static void parse_opt(int argc, char **argv)
591641
cfg_proto = SOCK_DGRAM;
592642
cfg_ipproto = IPPROTO_UDP;
593643
break;
644+
case 'v':
645+
cfg_delay_snd = strtoul(optarg, NULL, 10);
646+
break;
647+
case 'V':
648+
cfg_delay_ack = strtoul(optarg, NULL, 10);
649+
break;
594650
case 'x':
595651
cfg_show_payload = true;
596652
break;
@@ -651,6 +707,27 @@ static void resolve_hostname(const char *hostname)
651707
do_ipv6 &= have_ipv6;
652708
}
653709

710+
static void do_listen(int family, void *addr, int alen)
711+
{
712+
int fd, type;
713+
714+
type = cfg_proto == SOCK_RAW ? SOCK_DGRAM : cfg_proto;
715+
716+
fd = socket(family, type, 0);
717+
if (fd == -1)
718+
error(1, errno, "socket rx");
719+
720+
if (bind(fd, addr, alen))
721+
error(1, errno, "bind rx");
722+
723+
if (type == SOCK_STREAM && listen(fd, 10))
724+
error(1, errno, "listen rx");
725+
726+
/* leave fd open, will be closed on process exit.
727+
* this enables connect() to succeed and avoids icmp replies
728+
*/
729+
}
730+
654731
static void do_main(int family)
655732
{
656733
fprintf(stderr, "family: %s %s\n",
@@ -697,10 +774,17 @@ int main(int argc, char **argv)
697774
fprintf(stderr, "server port: %u\n", dest_port);
698775
fprintf(stderr, "\n");
699776

700-
if (do_ipv4)
777+
if (do_ipv4) {
778+
if (cfg_do_listen)
779+
do_listen(PF_INET, &daddr, sizeof(daddr));
701780
do_main(PF_INET);
702-
if (do_ipv6)
781+
}
782+
783+
if (do_ipv6) {
784+
if (cfg_do_listen)
785+
do_listen(PF_INET6, &daddr6, sizeof(daddr6));
703786
do_main(PF_INET6);
787+
}
704788

705-
return 0;
789+
return test_failed;
706790
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# Send packets with transmit timestamps over loopback with netem
5+
# Verify that timestamps correspond to netem delay
6+
7+
set -e
8+
9+
setup() {
10+
# set 1ms delay on lo egress
11+
tc qdisc add dev lo root netem delay 1ms
12+
13+
# set 2ms delay on ifb0 egress
14+
modprobe ifb
15+
ip link add ifb_netem0 type ifb
16+
ip link set dev ifb_netem0 up
17+
tc qdisc add dev ifb_netem0 root netem delay 2ms
18+
19+
# redirect lo ingress through ifb0 egress
20+
tc qdisc add dev lo handle ffff: ingress
21+
tc filter add dev lo parent ffff: \
22+
u32 match mark 0 0xffff \
23+
action mirred egress redirect dev ifb_netem0
24+
}
25+
26+
run_test_v4v6() {
27+
# SND will be delayed 1000us
28+
# ACK will be delayed 6000us: 1 + 2 ms round-trip
29+
local -r args="$@ -v 1000 -V 6000"
30+
31+
./txtimestamp ${args} -4 -L 127.0.0.1
32+
./txtimestamp ${args} -6 -L ::1
33+
}
34+
35+
run_test_tcpudpraw() {
36+
local -r args=$@
37+
38+
run_test_v4v6 ${args} # tcp
39+
run_test_v4v6 ${args} -u # udp
40+
run_test_v4v6 ${args} -r # raw
41+
run_test_v4v6 ${args} -R # raw (IPPROTO_RAW)
42+
run_test_v4v6 ${args} -P # pf_packet
43+
}
44+
45+
run_test_all() {
46+
run_test_tcpudpraw # setsockopt
47+
run_test_tcpudpraw -C # cmsg
48+
run_test_tcpudpraw -n # timestamp w/o data
49+
}
50+
51+
if [[ "$(ip netns identify)" == "root" ]]; then
52+
../../net/in_netns.sh $0 $@
53+
else
54+
setup
55+
run_test_all
56+
echo "OK. All tests passed"
57+
fi

0 commit comments

Comments
 (0)