selftests: return failure when timestamps can't be parsed
Subsystems (5)
PATCH 1/1 selftests: return failure when timestamps can't be parsed
From: Jason Xing <[email protected]>
When I was trying to modify the tx timestamping feature, I found that running "./txtimestamp -4 -C -L 127.0.0.1" didn't reflect the fact properly.
In this selftest file, we respectively test three tx generation flags. With the generation and report flag enabled, we expect that the timestamp must be returned to the userspace unless 1) generating the timestamp fails, 2) reporting the timestamp fails. So we should test if the timestamps can be read and parsed succuessfuly in txtimestamp.c, or else there is a bug in the kernel.
After adding the check so that running ./txtimestamp will reflect the result correctly like this if there is an error in kernel:
protocol: TCP payload: 10 server port: 9000
family: INET
test SND
USR: 1725458477 s 667997 us (seq=0, len=0)
Failed to parse timestamps
USR: 1725458477 s 718128 us (seq=0, len=0)
Failed to parse timestamps
USR: 1725458477 s 768273 us (seq=0, len=0)
Failed to parse timestamps
USR: 1725458477 s 818416 us (seq=0, len=0)Failed to parse timestamps
...
---
I'm not sure if I should also check if the cur->tv_sec or cur->tv_nsec is zero in __print_timestamp(). Could it be valid when either of them is zero?
--- tools/testing/selftests/net/txtimestamp.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/testing/selftests/net/txtimestamp.c +4 -0
@@ -358,6 +358,10 @@ static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)if (batch >1 )fprintf (stderr,"batched %d timestamps\n" , batch); +else if (!batch) { +fprintf (stderr,"Failed to parse timestamps\n" ); + test_failed = true; + } }static int recv_errmsg (int fd)
Signature · 1 line
2.37.3
Willem de Bruijn
maintainerQuoted 5 lines · Jason Xing
From: Jason Xing <[email protected]>
When I was trying to modify the tx timestamping feature, I found that running "./txtimestamp -4 -C -L 127.0.0.1" didn't reflect the fact properly.
Did not reflect what fact? Sorry, I don't entirely follow the issue you raise.
Quoted 5 lines
In this selftest file, we respectively test three tx generation flags. With the generation and report flag enabled, we expect that the timestamp must be returned to the userspace unless 1) generating the timestamp fails, 2) reporting the timestamp fails. So we should test if the timestamps can be read and parsed succuessfuly in
txtimestamp.c, or
typo: successfully
Quoted 17 lines
else there is a bug in the kernel.
After adding the check so that running ./txtimestamp will reflect the result correctly like this if there is an error in kernel:
protocol: TCP payload: 10 server port: 9000family: INET
test SNDUSR: 1725458477 s 667997 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 718128 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 768273 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 818416 us (seq=0, len=0)
Quoted 8 lines
Failed to parse timestamps
...Signed-off-by Jason Xing <[email protected]>---
I'm not sure if I should also check if thecur->tv_secorcur->tv_nsecis zero in__print_timestamp(). Could it be valid when either of them is zero?
tv_nsec can be zero. tv_sec cannot.
Quoted 9 lines
--- tools/testing/selftests/net/txtimestamp.c | 4 ++++ 1 file changed, 4 insertions(+)tools/testing/selftests/net/txtimestamp.c +0
-0
tools/testing/selftests/net/txtimestamp.c static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len) · line 364
Quoted 7 lines
tools/testing/selftests/net/txtimestamp.c +4
-0@@ -358,6 +358,10 @@ static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)if (batch >1 )fprintf (stderr,"batched %d timestamps\n" , batch); +else if (!batch) { +fprintf (stderr,"Failed to parse timestamps\n" ); + test_failed = true; + }
nit: if adding braces around one side of a branch, then add to both (all).
This is not so much a parsing failure as that no timestamps arrived.
More importantly, this function gets called also if recvmsg(fd, .., MSG_ERRQUEUE) returned 0:
if (ret >= 0) {
__recv_errmsg_cmsg(&msg, ret);That seems counterintuitive, as there is no data. But this was introduced with cfg_loop_nodata (SOF_TIMESTAMPING_OPT_TSONLY). When there may be packets looped, just 0B packets. In those cases we also expect timestamps.
But, can __recv_errmsg_cmsg now also be called when there truly is nothing on the error queue? It is a non-blocking read, after all.
Judging from
while (!recv_errmsg(fd)) {}The caller can. But if there is nothing waiting it returns -1 with EAGAIN:
ret = recvmsg(fd, &msg, MSG_ERRQUEUE);
if (ret == -1 && errno != EAGAIN)
error(1, errno, "recvmsg");So long story short, subject to a few nits your patch sounds okay to me (but it's not entirely trivial that that is so: sharing so that you also double check, thanks).
Quoted 6 lines
tools/testing/selftests/net/txtimestamp.c +0
-0}static int recv_errmsg (int fd)Signature · 1 line
2.37.3
Quoted 10 lines · Willem de Bruijn
Quoted 5 lines · Jason Xing
From: Jason Xing <[email protected]>
When I was trying to modify the tx timestamping feature, I found that running "./txtimestamp -4 -C -L 127.0.0.1" didn't reflect the fact properly.
Did not reflect what fact? Sorry, I don't entirely follow the issue you raise.
I was trying to say if someone writes a bug in the timestamping feature, the selftest probably returns pass value instead of failure.
I will explicitly report the case I met.
Quoted 8 lines
Quoted 6 lines
In this selftest file, we respectively test three tx generation flags. With the generation and report flag enabled, we expect that the timestamp must be returned to the userspace unless 1) generating the timestamp fails, 2) reporting the timestamp fails. So we should test if the timestamps can be read and parsed succuessfuly in
txtimestamp.c, ortypo: successfully
Will update it.
Quoted 21 lines
Quoted 21 lines
else there is a bug in the kernel.
After adding the check so that running ./txtimestamp will reflect the result correctly like this if there is an error in kernel:
protocol: TCP payload: 10 server port: 9000family: INET
test SNDUSR: 1725458477 s 667997 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 718128 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 768273 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 818416 us (seq=0, len=0)Failed to parse timestamps
...
Quoted 7 lines
Quoted 5 lines
Signed-off-by Jason Xing <[email protected]>---
I'm not sure if I should also check if thecur->tv_secorcur->tv_nsecis zero in__print_timestamp(). Could it be valid when either of them is zero?
tv_nseccan be zero.tv_seccannot.
Thanks. Now I am learning :)
Quoted 11 lines
Quoted 11 lines
--- tools/testing/selftests/net/txtimestamp.c | 4 ++++ 1 file changed, 4 insertions(+)tools/testing/selftests/net/txtimestamp.c +0
-0@@ -358,6 +358,10 @@ static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)
Quoted 8 lines
tools/testing/selftests/net/txtimestamp.c static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len) · line 364
Quoted 6 lines
tools/testing/selftests/net/txtimestamp.c +4
-0if (batch >1 )fprintf (stderr,"batched %d timestamps\n" , batch); +else if (!batch) { +fprintf (stderr,"Failed to parse timestamps\n" ); + test_failed = true; + }nit: if adding braces around one side of a branch, then add to both (all).
I see.
Quoted 6 lines
This is not so much a parsing failure as that no timestamps arrived.
More importantly, this function gets called also if
recvmsg(fd, .., MSG_ERRQUEUE)returned 0:
Quoted 7 lines
if (ret >= 0) { __recv_errmsg_cmsg(&msg, ret);That seems counterintuitive, as there is no data. But this was introduced with
cfg_loop_nodata(SOF_TIMESTAMPING_OPT_TSONLY). When there may be packets looped, just 0B packets. In those cases we also expect timestamps.
Right, It does make sense.
Quoted 11 lines
But, can
__recv_errmsg_cmsgnow also be called when there truly is nothing on the error queue? It is a non-blocking read, after all.Judging from
while (!recv_errmsg(fd)) {}The caller can. But if there is nothing waiting it returns -1 with EAGAIN:
Quoted 7 lines
ret = recvmsg(fd, &msg, MSG_ERRQUEUE); if (ret == -1 && errno != EAGAIN) error(1, errno, "recvmsg");So long story short, subject to a few nits your patch sounds okay to me (but it's not entirely trivial that that is so: sharing so that you also double check, thanks).
Thanks for pointing out this one. I will rewrite this patch/patch series tomorrow with your questions resolved.
Thanks,
Jason
Quoted 74 lines · Willem de Bruijn
Quoted 5 lines · Jason Xing
From: Jason Xing <[email protected]>
When I was trying to modify the tx timestamping feature, I found that running "./txtimestamp -4 -C -L 127.0.0.1" didn't reflect the fact properly.
Did not reflect what fact? Sorry, I don't entirely follow the issue you raise.
Quoted 5 lines
In this selftest file, we respectively test three tx generation flags. With the generation and report flag enabled, we expect that the timestamp must be returned to the userspace unless 1) generating the timestamp fails, 2) reporting the timestamp fails. So we should test if the timestamps can be read and parsed succuessfuly in
txtimestamp.c, ortypo: successfully
Quoted 17 lines
else there is a bug in the kernel.
After adding the check so that running ./txtimestamp will reflect the result correctly like this if there is an error in kernel:
protocol: TCP payload: 10 server port: 9000family: INET
test SNDUSR: 1725458477 s 667997 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 718128 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 768273 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 818416 us (seq=0, len=0)Quoted 8 lines
Failed to parse timestamps
...Signed-off-by Jason Xing <[email protected]>---
I'm not sure if I should also check if thecur->tv_secorcur->tv_nsecis zero in__print_timestamp(). Could it be valid when either of them is zero?
tv_nseccan be zero.tv_seccannot.Quoted 9 lines
--- tools/testing/selftests/net/txtimestamp.c | 4 ++++ 1 file changed, 4 insertions(+)tools/testing/selftests/net/txtimestamp.c +0
-0tools/testing/selftests/net/txtimestamp.c static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len) · line 364
Quoted 7 lines
tools/testing/selftests/net/txtimestamp.c +4
-0@@ -358,6 +358,10 @@ static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)if (batch >1 )fprintf (stderr,"batched %d timestamps\n" , batch); +else if (!batch) { +fprintf (stderr,"Failed to parse timestamps\n" ); + test_failed = true; + }nit: if adding braces around one side of a branch, then add to both (all).
This is not so much a parsing failure as that no timestamps arrived.
More importantly, this function gets called also if
recvmsg(fd, .., MSG_ERRQUEUE)returned 0:if (ret >= 0) { __recv_errmsg_cmsg(&msg, ret);
Quoted 7 lines
That seems counterintuitive, as there is no data. But this was introduced with
cfg_loop_nodata(SOF_TIMESTAMPING_OPT_TSONLY). When there may be packets looped, just 0B packets. In those cases we also expect timestamps.But, can
__recv_errmsg_cmsgnow also be called when there truly is nothing on the error queue? It is a non-blocking read, after all.
Today I re-read this paragraph. I think we were just past each other.
I would like to check that if the reporting timestamp with someone's patch applied someday wouldn't work, the txtimestamp should return failure to warn the submitter. That is to say, we succeed to generate the skb with timestamp but failed to report it like this:
net/ipv4/tcp.c +0 -0
@@ -2274,7 +2274,7 @@ void tcp_recv_timestamp(struct msghdr *msg,
const struct sock *sk,
}
}- if (READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_SOFTWARE)
+ if (!(READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_SOFTWARE))
has_timestamping = true;
else
tss->ts[0] = (struct timespec64) {0};which I intentionally wrote is used to show one stupid bug as an example. The txtimestamp test should spot it :)
Thanks,
Jason
Quoted 105 lines · Jason Xing
Quoted 74 lines · Willem de Bruijn
Quoted 5 lines · Jason Xing
From: Jason Xing <[email protected]>
When I was trying to modify the tx timestamping feature, I found that running "./txtimestamp -4 -C -L 127.0.0.1" didn't reflect the fact properly.
Did not reflect what fact? Sorry, I don't entirely follow the issue you raise.
Quoted 5 lines
In this selftest file, we respectively test three tx generation flags. With the generation and report flag enabled, we expect that the timestamp must be returned to the userspace unless 1) generating the timestamp fails, 2) reporting the timestamp fails. So we should test if the timestamps can be read and parsed succuessfuly in
txtimestamp.c, ortypo: successfully
Quoted 17 lines
else there is a bug in the kernel.
After adding the check so that running ./txtimestamp will reflect the result correctly like this if there is an error in kernel:
protocol: TCP payload: 10 server port: 9000family: INET
test SNDUSR: 1725458477 s 667997 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 718128 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 768273 us (seq=0, len=0) Failed to parse timestamps USR: 1725458477 s 818416 us (seq=0, len=0)Quoted 8 lines
Failed to parse timestamps
...Signed-off-by Jason Xing <[email protected]>---
I'm not sure if I should also check if thecur->tv_secorcur->tv_nsecis zero in__print_timestamp(). Could it be valid when either of them is zero?
tv_nseccan be zero.tv_seccannot.Quoted 9 lines
--- tools/testing/selftests/net/txtimestamp.c | 4 ++++ 1 file changed, 4 insertions(+)tools/testing/selftests/net/txtimestamp.c +0
-0tools/testing/selftests/net/txtimestamp.c static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len) · line 364
Quoted 7 lines
tools/testing/selftests/net/txtimestamp.c +4
-0@@ -358,6 +358,10 @@ static void __recv_errmsg_cmsg(struct msghdr *msg, int payload_len)if (batch >1 )fprintf (stderr,"batched %d timestamps\n" , batch); +else if (!batch) { +fprintf (stderr,"Failed to parse timestamps\n" ); + test_failed = true; + }nit: if adding braces around one side of a branch, then add to both (all).
This is not so much a parsing failure as that no timestamps arrived.
More importantly, this function gets called also if
recvmsg(fd, .., MSG_ERRQUEUE)returned 0:if (ret >= 0) { __recv_errmsg_cmsg(&msg, ret);Quoted 7 lines
That seems counterintuitive, as there is no data. But this was introduced with
cfg_loop_nodata(SOF_TIMESTAMPING_OPT_TSONLY). When there may be packets looped, just 0B packets. In those cases we also expect timestamps.But, can
__recv_errmsg_cmsgnow also be called when there truly is nothing on the error queue? It is a non-blocking read, after all.Today I re-read this paragraph. I think we were just past each other.
I would like to check that if the reporting timestamp with someone's patch applied someday wouldn't work, the txtimestamp should return failure to warn the submitter. That is to say, we succeed to generate the skb with timestamp but failed to report it like this:
net/ipv4/tcp.c +1
-1@@ -2274,7 +2274,7 @@ void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk, } } -if (READ_ONCE (sk->sk_tsflags) &SOF_TIMESTAMPING_SOFTWARE ) +if (!(READ_ONCE (sk->sk_tsflags) &SOF_TIMESTAMPING_SOFTWARE )) has_timestamping = true;else
Quoted 1 line
tss->ts[0] = (struct timespec64) {0};If so, rxtimestamp test will fail. Let me correct myself here.
Quoted 3 lines
which I intentionally wrote is used to show one stupid bug as an example. The txtimestamp test should spot it :)
Sorry, not in tcp_recv_timestamp, but like in __sock_recv_timestamp:
j@@ -946,7 +946,7 @@ void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
memset(&tss, 0, sizeof(tss));
tsflags = READ_ONCE(sk->sk_tsflags);
- if ((tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
+ if (!(tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
empty = 0;
if (shhwtstamps &&This error/bug cannot be noticed by txtimestamp before this patch.
It's just an example which helps me clarify my thoughts.
Quoted 3 lines
Thanks,
Jason